Parsing a JSON array using Json.Net

前端 未结 2 1234
挽巷
挽巷 2020-11-28 04:53

I\'m working with Json.Net to parse an array. What I\'m trying to do is to pull the name/value pairs out of the array and assign them to specific variables while parsing the

相关标签:
2条回答
  • 2020-11-28 05:17

    You can get at the data values like this:

    string json = @"
    [ 
        { ""General"" : ""At this time we do not have any frequent support requests."" },
        { ""Support"" : ""For support inquires, please see our support page."" }
    ]";
    
    JArray a = JArray.Parse(json);
    
    foreach (JObject o in a.Children<JObject>())
    {
        foreach (JProperty p in o.Properties())
        {
            string name = p.Name;
            string value = (string)p.Value;
            Console.WriteLine(name + " -- " + value);
        }
    }
    

    Fiddle: https://dotnetfiddle.net/uox4Vt

    0 讨论(0)
  • 2020-11-28 05:25

    Use Manatee.Json https://github.com/gregsdennis/Manatee.Json/wiki/Usage

    And you can convert the entire object to a string, filename.json is expected to be located in documents folder.

            var text = File.ReadAllText("filename.json");
            var json = JsonValue.Parse(text);
    
            while (JsonValue.Null != null)
            {
                Console.WriteLine(json.ToString());
    
            }
            Console.ReadLine();
    
    0 讨论(0)
提交回复
热议问题