C# 在c中迭代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: [

我已从服务返回以下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)"
    ]
  }
}
  • 我需要在c#中创建一个列表,列出“suggestion”元素中的内容。最好的方法是什么
  • 哪些元素没有被“”包围。所有json元素不都应该被“”包围吗? 谢谢
  • 编辑: 这是基于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()));
                    }
    
     }
    
    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"
        ]
      }
    }
    
  • 是的,所有键都应该用双引号括起来
  • 你的“建议”结构毫无意义。。。你不应该有一个定义良好的“建议”对象数组吗?现在,您已经有了一个字符串(“at”、“collation”)和其他json对象(带有numFound的对象,等等)的混合数组
  • 在那里有一个字符串“at”的目的是什么?它不是一个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()));
               }
           }
    

    这看起来像是一个SOLR响应——除了属性名称/键应该被引用之外,这是正确的,实际上——建议通常有更多的数据——在一个对象数组中,您是正确的。我想问的是如何解析SOLR答案的建议。我已经编辑了我的答案。我做了一些改进,并将其添加到问题中。