C# 在c中迭代json元素#
我已从服务返回以下json:C# 在c中迭代json元素#,c#,json,dynamic,C#,Json,Dynamic,我已从服务返回以下json: { responseHeader: { status: 0, QTime: 1 }, spellcheck: { suggestions: [ "at", { numFound: 2, startOffset: 0, endOffset: 2, suggestion: [
{
responseHeader: {
status: 0,
QTime: 1
},
spellcheck: {
suggestions: [
"at",
{
numFound: 2,
startOffset: 0,
endOffset: 2,
suggestion: [
"at least five tons of glitter alone had gone into it before them and",
"at them the designer of the gun had clearly not been instructed to beat"
]
},
"collation",
"(at least five tons of glitter alone had gone into it before them and)"
]
}
}
dynamic resultChildren = result.spellcheck.suggestions.Children();
foreach (dynamic child in resultChildren)
{
var suggestionObj = child as JObject;
if (suggestionObj != null)
{
var subArr = suggestionObj.Value<JArray>("suggestion");
strings.AddRange(subArr.Select(suggestion => suggestion.ToString()));
}
}
dynamic resultChildren=result.spellcheck.suggestions.Children();
foreach(结果子项中的动态子项)
{
var suggestionbj=作为作业对象的子对象;
if(suggestionObj!=null)
{
var Subar=建议价值(“建议”);
strings.AddRange(subar.Select(suggestion=>suggestion.ToString());
}
}
json字符串出现问题:
{
"responseHeader": {
"status": 0,
"QTime": 1
},
"spellcheck": {
"suggestions": [
"at",
{
"numFound": 2,
"startOffset": 0,
"endOffset": 2,
"suggestion": ["at least five tons of glitter alone had gone into it before them and", "at them the designer of the gun had clearly not been instructed to beat"]
},
"collation"
]
}
}
JObject obj = JObject.Parse(json);
var suggestionsArr = obj["spellcheck"].Value<JArray>("suggestions");
var strings = new List<string>();
foreach (var suggestionElem in suggestionsArr)
{
var suggestionObj = suggestionElem as JObject;
if (suggestionObj != null)
{
var subArr = suggestionObj.Value<JArray>("suggestion");
strings.AddRange(subArr.Select(suggestion => suggestion.ToString()));
}
}
这看起来像是一个SOLR响应——除了属性名称/键应该被引用之外,这是正确的,实际上——建议通常有更多的数据——在一个对象数组中,您是正确的。我想问的是如何解析SOLR答案的建议。我已经编辑了我的答案。我做了一些改进,并将其添加到问题中。