Reputation: 1247
I need to query for and get the matched JSON string. Following is my JSON:
I need to query the JSON I receive in the HTTP RESPONSE
, match the JSON where code=2
, then extract the text=Jenny kisworth
JSON
[
{
"code":1234,
"parentCode":9898,
"language":{
"lookup": "IN",
"code": 1
},
"parentType": "Patient",
"text": "James Berth"
},
{
"code":4567,
"parentCode":8989,
"language":{
"lookup": "IN",
"code": 1
},
"parentType": "Patient",
"text": "James Bond"
},
{
"code":89101,
"parentCode":2525,
"language":{
"lookup": "OUT",
"code": 2
},
"parentType": "Patient",
"text": "Jenny kisworth"
}
]
CODE:
public class JSonData
{
[Newtonsoft.Json.JsonProperty("code")]
public string code { get; set; }
[Newtonsoft.Json.JsonProperty("language")]
public List<Datum> language { get; set; }
}
public class Datum
{
public string lookup { get; set; }
public int code { get; set; }
}
//only posting code relevant to the subject
HttpResponseMessage responseCode = client.GetAsync(codeParameters).Result;
if (responseCode.IsSuccessStatusCode)
{
var dataObjects = responseAlternateTitles.Content.ReadAsStringAsync();
dataObjects.Wait();
string dataObjectsString = dataObjects.Result.ToString();
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JSonData>>(dataObjectsString);
}
In the above I get an error: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List
1[BCMTest.Datum]' because the type requires a JSON array`
Upvotes: 0
Views: 1064
Reputation: 1247
public class Language
{
public string lookup { get; set; }
public int code { get; set; }
}
public class JSonData
{
[Newtonsoft.Json.JsonProperty("code")]
public string code { get; set; }
[Newtonsoft.Json.JsonProperty("parentCode")]
public int parentCode { get; set; }
[Newtonsoft.Json.JsonProperty("language")]
public Language language { get; set; }
[Newtonsoft.Json.JsonProperty("parentType")]
public string parentType { get; set; }
[Newtonsoft.Json.JsonProperty("text")]
public string text { get; set; }
}
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JSonData>>(dataObjectsString);
var filtereddata = data.Where(s => s.language.code.Equals(2));
Upvotes: 1
Reputation: 2212
You are getting an error because your JSON doesnot have a object of array in language as you are expecting in your class object.
Change the JSonData class -> language
public class JSonData
{
[Newtonsoft.Json.JsonProperty("code")]
public string code { get; set; }
[Newtonsoft.Json.JsonProperty("language")]
public Datum language { get; set; }
}
Upvotes: 1
Reputation: 81533
Your classes should look more like this, how do i know? http://json2csharp.com/
public class Language
{
public string lookup { get; set; }
public int code { get; set; }
}
public class JSonData
{
public int code { get; set; }
public int parentCode { get; set; }
public Language language { get; set; }
public string parentType { get; set; }
public string text { get; set; }
}
...
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JSonData>>(dataObjectsString);
Upvotes: 1