来自restful API的响应会返回一些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());
}
}
......笨拙乏味。肯定有更好的办法。有线索吗?
答案 0 :(得分:0)
您可以使用Json.Net&#39; LINQ-to-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();