Reputation: 109
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;
}
}
}
Upvotes: 1
Views: 2418
Reputation: 32703
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);
Upvotes: 2