我需要查询并获取匹配的JSON字符串。以下是我的JSON:
我需要查询在HTTP RESPONSE
中收到的JSON,将JSON与code=2
匹配,然后提取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"
}
]
代码:
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);
}
在上面,我得到一个错误:Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List
1 [BCMTest.Datum]',因为该类型需要JSON数组。
答案 0 :(得分:1)
您的课程应该看起来像这样,我怎么知道? 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);
答案 1 :(得分:1)
您收到错误消息,因为您的JSON不具有您在类对象中所期望的语言数组对象。
更改JSonData class -> language
public class JSonData
{
[Newtonsoft.Json.JsonProperty("code")]
public string code { get; set; }
[Newtonsoft.Json.JsonProperty("language")]
public Datum language { get; set; }
}
答案 2 :(得分:1)
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));