我试着像这样解析一些JSON:
{
"results": [
"MLU413843206",
"MLU413841098",
"MLU413806325",
"MLU413850890",
"MLU413792303",
"MLU413843455",
"MLU413909270",
"MLU413921617",
"MLU413921983",
"MLU413924015",
"MLU413924085"
]
}
一切都很好,直到我尝试自己获取值,例如:
// The JSON is shown above
var jsonResp = JObject.Parse(json);
var items = jsonResp["results"].Children();
我不知道如何获取值,每个都转换为字符串。有人知道怎么做吗?
答案 0 :(得分:1)
你已经到了一半。您可以使用Select()
命名空间中的System.Linq
方法将IEnumerable<JToken>
方法返回的Children()
投影到IEnumerable<string>
。在那里,您可以使用foreach
循环显示值,或使用List<string>
(或两者)将值放入ToList()
。
string json = @"
{
""results"": [
""MLU413843206"",
""MLU413841098"",
""MLU413806325"",
""MLU413850890"",
""MLU413792303"",
""MLU413843455"",
""MLU413909270"",
""MLU413921617"",
""MLU413921983"",
""MLU413924015"",
""MLU413924085""
]
}";
JObject jsonResp = JObject.Parse(json);
List<string> items = jsonResp["results"].Children()
.Select(t => t.ToString())
.ToList();
foreach (string item in items)
{
Console.WriteLine(item);
}