解析JSON返回是C#中的对象数组

时间:2014-07-22 13:26:00

标签: c# json

我正在调用Web服务,而JSON返回是一个对象数组。通常我使用Json.NET和Visual Studio来处理其余的事情,但在这种情况下,Visual Studio期望一个对象而不是一个对象数组,我不知道如何让它正确解析。

正常拨打电话之后我会做的是:

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

然后使用return serviceResponse.clientID


以下是一个示例回复:

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

}

我希望它能解析出来,以便我可以使用返回serviceResponse[0].clientID,但我无法弄清楚如何让VS识别它是一个数组返回而不是单个对象。


如果我尝试以下方法:

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

我收到此错误:

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.

我也尝试了以下内容:

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

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

当响应中有多个对象时,这会有效,但当响应中只有一个对象时,我会收到此错误:

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.

这里要求的是VS从我给出的xsd文件生成的Client.orderSummary:

/// <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;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您要反序列化为单个Client.ordersummary对象。要反序列化为Client.ordersummary列表,请执行以下操作:

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