Iterating over JSON object in C#

后端 未结 3 1809
死守一世寂寞
死守一世寂寞 2020-11-28 05:05

I am using JSON.NET in C# to parse a response from the Klout API. My response is like this:

[
  {
    \"id\": \"5241585099662481339\",
    \"displayName\":          


        
相关标签:
3条回答
  • 2020-11-28 05:46
    dynamic dynJson = JsonConvert.DeserializeObject(json);
    foreach (var item in dynJson)
    {
        Console.WriteLine("{0} {1} {2} {3}\n", item.id, item.displayName, 
            item.slug, item.imageUrl);
    }
    

    or

    var list = JsonConvert.DeserializeObject<List<MyItem>>(json);
    
    public class MyItem
    {
        public string id;
        public string displayName;
        public string name;
        public string slug;
        public string imageUrl;
    }
    
    0 讨论(0)
  • 2020-11-28 05:48

    You can use the JsonTextReader to read the JSON and iterate over the tokens:

    using (var reader = new JsonTextReader(new StringReader(jsonText)))
    {
        while (reader.Read())
        {
            Console.WriteLine("{0} - {1} - {2}", 
                              reader.TokenType, reader.ValueType, reader.Value);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 05:54

    This worked for me, converts to nested JSON to easy to read YAML

        string JSONDeserialized {get; set;}
        public int indentLevel;
    
        private bool JSONDictionarytoYAML(Dictionary<string, object> dict)
        {
            bool bSuccess = false;
            indentLevel++;
    
            foreach (string strKey in dict.Keys)
            {
                string strOutput = "".PadLeft(indentLevel * 3) + strKey + ":";
                JSONDeserialized+="\r\n" + strOutput;
    
                object o = dict[strKey];
                if (o is Dictionary<string, object>)
                {
                    JSONDictionarytoYAML((Dictionary<string, object>)o);
                }
                else if (o is ArrayList)
                {
                    foreach (object oChild in ((ArrayList)o))
                    {
                        if (oChild is string)
                        {
                            strOutput = ((string)oChild);
                            JSONDeserialized += strOutput + ",";
                        }
                        else if (oChild is Dictionary<string, object>)
                        {
                            JSONDictionarytoYAML((Dictionary<string, object>)oChild);
                            JSONDeserialized += "\r\n";  
                        }
                    }
                }
                else
                {
                    strOutput = o.ToString();
                    JSONDeserialized += strOutput;
                }
            }
    
            indentLevel--;
    
            return bSuccess;
    
        }
    

    usage

            Dictionary<string, object> JSONDic = new Dictionary<string, object>();
            JavaScriptSerializer js = new JavaScriptSerializer();
    
              try {
    
                JSONDic = js.Deserialize<Dictionary<string, object>>(inString);
                JSONDeserialized = "";
    
                indentLevel = 0;
                DisplayDictionary(JSONDic); 
    
                return JSONDeserialized;
    
            }
            catch (Exception)
            {
                return "Could not parse input JSON string";
            }
    
    0 讨论(0)
提交回复
热议问题