Error starting Newtonsoft.json: the array was not a one-dimensional array

I get an error when trying to serialize object products.

Product product = new Product();

product.Name = "Apple";

product.Expiry = new DateTime(2008, 12, 28);

product.Price = 3.99M;

product.Sizes = new string[3,2] { {"Small","40"}, {"Medium","44"}, {"Large","50"} };



string json = JsonConvert.SerializeObject(product);//this line is throwing an error


Array was not a one-dimensional array

Is there a way to serialize a two-dimensional array with Newtonsoft.json

Thanks at Advance. SIA

+3
source share
2 answers

Json.NET does not support multidimensional arrays. Use a jagged array instead.

http://www.c-sharpcorner.com/uploadfile/mahesh/workingwitharrays11232005064036am/workingwitharrays.aspx

+3
source

Does newtonsoft support serialization of anonymous objects? If so, you can try:

product.Sizes = new {Small = 40, Medium = 44, Large = 50};

You will Product.Sizesonly need to change toobject

0
source

Source: https://habr.com/ru/post/1742020/


All Articles