views:

244

answers:

2

Hi everybody,

I am getting an error when trying to serialize an 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 any way to serialize a two dimensional array with Newtonsoft.json

Thanks in Advance. SIA

A: 

Does newtonsoft support serializing anonymous objects? If so you can try:

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

You'll need to change Product.Sizes to just be an object

zincorp
+1  A: 

Json.NET doesn't support multi-dimensional arrays. Use a jagged array instead.

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

James Newton-King
Thanks a lot King!!I have been struggling to accomplish this since last 24 hours
SIA