24

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?

1

2 Answers 2

56

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

var myobj = jsSerializer.Deserialize<List<BigCommerceOrderProduct>>(jsonData);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that makes sense now. Let me try again and report back.
1

This little proggy works fine for me. Could be something unexpected in the response stream.

The json output is: {"Data":[{"Id":33,"Order_id":230025},{"Id":34,"Order_id":230025}]}

    JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); 

    BigCommerceOrderProducts a = new BigCommerceOrderProducts();
    a.Data = new List<BigCommerceOrderProduct>();
    BigCommerceOrderProduct b = new BigCommerceOrderProduct();
    b.Id = 33;
    b.Order_id = 230025;

    a.Data.Add(b);

    b = new BigCommerceOrderProduct();
    b.Id = 34;
    b.Order_id = 230025;

    a.Data.Add(b);

    string x = jsSerializer.Serialize(a);
    Console.WriteLine(x);

    a = jsSerializer.Deserialize<BigCommerceOrderProducts>(x);

    Console.WriteLine(a.Data[0].Order_id);

    Console.ReadLine();

3 Comments

Chris, "[ {\"id\":33,\"order_id\":230025,...}, {\"id\":34,\"order_id\":230025,...} ]" is an array, on the other hand {"Data":[{"Id":33,"Order_id":230025},{"Id":34,"Order_id":230025}]} is an object containing an array. Two different things.
Yes, I know the difference. Either the OP left off the "Data": part or the OP is attempting to deserialize an array to List<>. So either the json returned in the response is incorrect, or the OP needs to deserialize to BigCommerceOrderProduct[] instead.
See my answer, response is correct and can be deserialized to List. (I never post a code before running on my machine or I mark it as UNTESTED)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.