2010-04-20 47 views
3

嘗試序列化對象產品時出現錯誤。Newtonsoft.json拋出錯誤:數組不是一維數組

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 

有沒有什麼辦法來序列二維陣列Newtonsoft.json

在此先感謝。 SIA

回答

0

newtonsoft是否支持序列化匿名對象?如果是這樣,你可以嘗試:

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

你需要改變Product.Sizes僅僅是object