1

I am making a call to a web service and the JSON return is an array of objects. Normally I use Json.NET and Visual Studio takes care of the rest, but in this case Visual studio is expecting a single object instead of an array of objects and I'm not sure how to get it to parse correctly.

After making the call normally what I would do is:

var serviceResponse = JsonConvert.DeserializeObject<Client.orderSummary>(response.Content);

then use the return as serviceResponse.clientID


Here is an example response:

{
"list": [
    {
        "clientId": "6974",
        "orderId": "33305",
        "itemsOrdered": {
            "id": [
                156751
            ]
        }
    },
    {
        "clientId": "6974",
        "orderId": "11288",
        "itemsOrdered": {
            "id": [
                156751
            ]
        }
    },
    {
        "clientId": "6974",
        "orderId": "27474",
        "itemsOrdered": {
            "id": [
                108801
            ]
        }
    }
]

}

I expect it to be parsing out so that I can use the return as serviceResponse[0].clientID but I can't figure out how to get VS to recognize it is an array coming back instead of a single object.


If I try the following:

var serviceResponse = JsonConvert.DeserializeObject<List<Client2.clientCaseSummary>>(response.Content);

I get this error:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Client2.clientCaseSummary]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

I have also tried the following:

var listObject = JObject.Parse(response.Content);

var serviceResponse = JsonConvert.DeserializeObject<List<Client2.clientCaseSummary>>(listObject["list"].ToString());

This works when there is more than one object in the response, but when there is only one object in the response I get this error:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.String[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

As requested here is the Client.orderSummary that VS generate from the xsd files I was given:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://xxxxx")]
public partial class orderSummary : codexElement {

    private string clientIdField;

    private string orderIdField;

    private string[] itemsOrderedField;


    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string clientId {
        get {
            return this.clientIdField;
        }
        set {
            this.clientIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string orderId {
        get {
            return this.orderIdField;
        }
        set {
            this.orderIdField = value;
        }
    }


    /// <remarks/>
    [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlArrayItemAttribute("id", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer", IsNullable=false)]
    public string[] itemsOrdered{
        get {
            return this.itemsOrderedField;
        }
        set {
            this.itemsOrderedField = value;
        }
    }
}
Slowstuff
  • 109
  • 1
  • 6
  • `Visual Studio takes care of the rest` what are you talking about? Visual Studio is the IDE, not a magician. – mason Jul 22 '14 at 13:27
  • Have you done any research? [Does this work?](http://stackoverflow.com/questions/18192357/deserializing-json-object-array-with-json-net) – Mat J Jul 22 '14 at 13:28
  • Also there is this implementation: http://stackoverflow.com/questions/18538428/loading-a-json-file-into-c-sharp-program – ford prefect Jul 22 '14 at 13:30
  • I did do reasearch, and had tried the link Mathew mentioned, but it returns "Cannot deserialize the current JSON object" error. The link is specifically about that, but I don't know what the new data model thing means. Where to put it or what to do. Sorry for having little to no experience at dealing with this. – Slowstuff Jul 22 '14 at 13:42
  • Your JSON is not valid. I verified that using [JSON Lint](http://jsonlint.com/). You're missing the closing curly brace for the `itemsOrdered` and you should also have curly braces around the entire object. – mason Jul 22 '14 at 14:55
  • Sorry, I had to make some changes to it before posting it. I have verified the original return is good, will post a fixed copy. – Slowstuff Jul 22 '14 at 15:01

1 Answers1

2

You're deserializing to a single Client.ordersummary object. To deserialize to a list of Client.ordersummary, do this:

var serviceResponse = JsonConvert.DeserializeObject<List<Client.orderSummary>>(response.Content);
mason
  • 31,774
  • 10
  • 77
  • 121
  • Obviously a duplicate question. – Mat J Jul 22 '14 at 13:29
  • @Mathew Appreciate the notification, but sometimes people don't understand how the duplicate question relates to their own, and need a customized response. This is particularly common when the question involves debugging. – mason Jul 22 '14 at 13:33
  • Agreed, But they should demonstrate that they have done their research. – Mat J Jul 22 '14 at 13:34
  • @Matthew Obviously, but that would eliminate 9/10ths of the questions on SO. Programming is difficult for newcomers, and can be so overwhelming you don't even know where to start looking. You really don't need to have this discussion with me here, as it's unrelated to the answer. – mason Jul 22 '14 at 13:37
  • Sorry for not meeting your standards, but the previous information I found didn't help, or more so I suppose I don't understand where to go in code to fix the problem. – Slowstuff Jul 22 '14 at 13:44
  • This is the error I get. "Cannot deserialize the current JSON object" ... "To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type" – Slowstuff Jul 22 '14 at 13:48
  • @Slowstuff You need to update your question to include the relevant parts of `Client.orderSummary` so we can verify that you're deserializing into a compatible type. Also, update your JSON to show more than one object (3 would be a good representative number). – mason Jul 22 '14 at 13:59
  • Done, sorry if this is really simple and I'm just not getting it. – Slowstuff Jul 22 '14 at 14:39