3

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

SIA
  • 773
  • 3
  • 11
  • 22

2 Answers2

3

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
  • 48,174
  • 24
  • 109
  • 130
  • Thanks a lot King!! I have been struggling to accomplish this since last 24 hours – SIA Apr 21 '10 at 11:15
0

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
  • 3,284
  • 1
  • 15
  • 18