我从服务中返回了以下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",
"(at least five tons of glitter alone had gone into it before them and)"
]
}
}
修改: 这是基于dcastro答案
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()));
}
}
答案 0 :(得分:1)
json字符串问题:
修改强>
这应该有效:
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()));
}
}
假设以下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"
]
}
}