c# - 反序列化 JSON 对象和查询
问题描述
我需要查询并获取匹配的 JSON 字符串。以下是我的 JSON:
我需要查询我收到的HTTP RESPONSE
JSON,匹配 JSON where 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 数组`
解决方案
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));
推荐阅读
- javascript - javascript--遍历json并在末尾添加累积数
- ocr - 限制 Tesseract 中的空间大小
- game-maker-studio-2 - 低分辨率背景显得模糊
- node.js - POST 请求因身份验证范围不足而失败,而 GET 请求成功
- arrays - 动态列表内的动态列表
- webpack - 使用 webpack mini-css-extract-plugin 插件时出错
- c++ - 显式类型转换的 C++ 转换表示法(C 样式转换)和 static_cast 的多种解释
- r - 使用 case_when 填写一个字符串
- apache-kafka - KAFKA - 如何重新平衡可用经纪人的分区
- amazon-web-services - emr-dynamodb-connector 是否并行读取数据