Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScriptSerializer.Deserialize array

Tags:

I'm having trouble deserializing an array in .NET MVC3, any help would be appreciated.

Here's the code snippet:

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) using (StreamReader reader = new StreamReader(response.GetResponseStream())) {     JavaScriptSerializer jsSerializer = new JavaScriptSerializer();     string jsonData = reader.ReadToEnd();     result = (BigCommerceOrderProducts)jsSerializer.Deserialize<BigCommerceOrderProducts>(jsonData); } 

Here's the subset of the data string returned by JSON as jsonData. I've remove extra fields.

"[ {\"id\":33,\"order_id\":230025,...}, {\"id\":34,\"order_id\":230025,...} ]" 

Here are the objects:

[Serializable] public class BigCommerceOrderProducts {     public List<BigCommerceOrderProduct> Data { get; set; } }  [Serializable] public class BigCommerceOrderProduct {     public int Id { get; set; }     public int Order_id { get; set; }     ... } 

I'm getting this error:

"Type 'Pxo.Models.BigCommerce.BigCommerceOrderProducts' is not supported for deserialization of an array. 

Any ideas?

like image 470
netwire Avatar asked Jan 27 '12 13:01

netwire


1 Answers

You should deserialize your json string to type List<BigCommerceOrderProduct>. No need for BigCommerceOrderProducts class

var myobj = jsSerializer.Deserialize<List<BigCommerceOrderProduct>>(jsonData); 
like image 109
L.B Avatar answered Nov 09 '22 04:11

L.B