来自restful的响应返回一些JSON:
{
"meta": {
"href": "http://localhost:54398/"
},
"busPartner": {
"href": "http://localhost:54398/BusPartner",
"rel": [
"collection"
]
},
"busPartnerType": {
"href": "http://localhost:54398/BusPartnerType",
"rel": [
"collection"
]
},
"busPartnerPossAttrib": {
"href": "http://localhost:54398/BusPartnerPossAttribs",
"rel": [
"collection"
]
}
}我正在尝试从JSON中提取一个href值的列表。虽然我可以按照下面的方式使用JsonTextReader并将我需要的值从结果列表中取出来.
IList<string> tt = new List<string>();
JsonTextReader reader = new JsonTextReader(new StringReader(response));
while (reader.Read())
{
if (reader.Value != null)
{
tt.Add( reader.TokenType + " " + reader.Value);
}
else
{
tt.Add(reader.TokenType.ToString());
}
}...it既笨拙又乏味。一定有更好的办法。有什么线索吗?
发布于 2017-10-22 06:04:16
您可以使用Json.Net的LINQ到JSON API (JObjects)提取列表:
List<string> tt = JObject.Parse(response)
.Descendants()
.OfType<JProperty>()
.Where(jp => jp.Name == "href" && jp.Value.Type == JTokenType.String)
.Select(jp => (string)jp.Value)
.ToList();https://stackoverflow.com/questions/46870063
复制相似问题