Accessing all items in the JToken

后端 未结 3 1095
[愿得一人]
[愿得一人] 2020-12-02 18:39

I have a json block like this:

{
    \"ADDRESS_MAP\":{

        \"ADDRESS_LOCATION\":{
            \"type\":\"separator\",
            \"name\":\"Address\",
         


        
相关标签:
3条回答
  • 2020-12-02 18:54

    You can cast your JToken to a JObject and then use the Properties() method to get a list of the object properties. From there, you can get the names rather easily.

    Something like this:

    string json =
    @"{
        ""ADDRESS_MAP"":{
    
            ""ADDRESS_LOCATION"":{
                ""type"":""separator"",
                ""name"":""Address"",
                ""value"":"""",
                ""FieldID"":40
            },
            ""LOCATION"":{
                ""type"":""locations"",
                ""name"":""Location"",
                ""keyword"":{
                    ""1"":""LOCATION1""
                },
                ""value"":{
                    ""1"":""United States""
                },
                ""FieldID"":41
            },
            ""FLOOR_NUMBER"":{
                ""type"":""number"",
                ""name"":""Floor Number"",
                ""value"":""0"",
                ""FieldID"":55
            },
            ""self"":{
                ""id"":""2"",
                ""name"":""Address Map""
            }
        }
    }";
    
    JToken outer = JToken.Parse(json);
    JObject inner = outer["ADDRESS_MAP"].Value<JObject>();
    
    List<string> keys = inner.Properties().Select(p => p.Name).ToList();
    
    foreach (string k in keys)
    {
        Console.WriteLine(k);
    }
    

    Output:

    ADDRESS_LOCATION
    LOCATION
    FLOOR_NUMBER
    self
    
    0 讨论(0)
  • 2020-12-02 18:54

    If you know the structure of the json that you're receiving then I'd suggest having a class structure that mirrors what you're receiving in json.

    Then you can call its something like this...

    AddressMap addressMap = JsonConvert.DeserializeObject<AddressMap>(json);
    

    (Where json is a string containing the json in question)

    If you don't know the format of the json you've receiving then it gets a bit more complicated and you'd probably need to manually parse it.

    check out http://www.hanselman.com/blog/NuGetPackageOfTheWeek4DeserializingJSONWithJsonNET.aspx for more info

    0 讨论(0)
  • 2020-12-02 19:11

    In addition to the accepted answer I would like to give an answer that shows how to iterate directly over the Newtonsoft collections. It uses less code and I'm guessing its more efficient as it doesn't involve converting the collections.

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    //Parse the data
    JObject my_obj = JsonConvert.DeserializeObject<JObject>(your_json);
    
    foreach (KeyValuePair<string, JToken> sub_obj in (JObject)my_obj["ADDRESS_MAP"])
    {
        Console.WriteLine(sub_obj.Key);
    }
    

    I started doing this myself because JsonConvert automatically deserializes nested objects as JToken (which are JObject, JValue, or JArray underneath I think).

    I think the parsing works according to the following principles:

    • Every object is abstracted as a JToken

    • Cast to JObject where you expect a Dictionary

    • Cast to JValue if the JToken represents a terminal node and is a value

    • Cast to JArray if its an array

    • JValue.Value gives you the .NET type you need

    0 讨论(0)
提交回复
热议问题