简体   繁体   中英

JSON JObject Detect Multiple Array Objects

Is there a way to pull out all arrays from a Json Response, where i dont know the name of each array in the response.

I basically want to detect the number of array objects in the JSON, and then access each one individually.

I'm doing this dynamically, so one response may only contain the "employees" array, the next response may include both. The structure of both array's will always remain the same.

{
    "employees": [
        {
            "firstName": "John",
            "lastName": "Doe"
        },
        {
            "firstName": "Anna",
            "lastName": "Smith"
        },
        {
            "firstName": "Peter",
            "lastName": "Jones"
        }
    ],
    "employers": [
        {
            "firstName": "John",
            "lastName": "Doe"
        },
        {
            "firstName": "Anna",
            "lastName": "Smith"
        },
        {
            "firstName": "Peter",
            "lastName": "Jones"
        }
    ]
}

there is an easier way to do this which does not involve creating classes.

You can use JObject/JArray (in your case jObject) and use JObject.Parse, which will return an JObject. that's a tree structure generated from the parsed json.

Probably more comfortable than creating classes for changing needs. Also there's an xpath like syntax to navigate through keys.

Update:

var so = "{\"employees\": [{\"firstName\": \"John\",\"lastName\": \"Doe\"},{\"firstName\": \"Anna\",\"lastName\": \"Smith\"},{\"firstName\": \"Peter\",\"lastName\": \"Jones\"}],\"employers\": [{\"firstName\": \"John\",\"lastName\": \"Doe\"},{\"firstName\": \"Anna\",\"lastName\": \"Smith\"},{\"firstName\": \"Peter\",\"lastName\": \"Jones\"}]}";
var parsed = JObject.Parse(so);
foreach (KeyValuePair<string, JToken> pair in parsed)
{
    Debug.WriteLine(pair.Key);
    foreach (JToken childObject in pair.Value)
    {
        Debug.WriteLine(childObject["firstName"]);
        Debug.WriteLine(childObject["lastName"]);
    }
}

    // Or with Syntax i talked about: 
foreach (var token in parsed.SelectTokens("employees/"))
{
    Debug.WriteLine(token["firstName"]);
    Debug.WriteLine(token["lastName"]);
}

First of all, according to your statement, as the structure of both arrays will always remain the same, we may define the structure as a List<FullName> ; given that,

public class FullName
{
    public string firstName { get; set; }
    public string lastName { get; set; }
}

Second, as you don't know the name of each array in the response in advance, a data structure like Dictionary may be appointed.

Fyki, I am using Json.Net here. So please do not forget to add the reference in advance.

Please see the code below:

// 'json' is in enquoted string format 
string  json = "{\"employees\": [{\"firstName\": \"John\",\"lastName\": \"Doe\"},{\"firstName\": \"Anna\",\"lastName\": \"Smith\"},{\"firstName\": \"Peter\",\"lastName\": \"Jones\"}],\"employers\": [{\"firstName\": \"John\",\"lastName\": \"Doe\"},{\"firstName\": \"Anna\",\"lastName\": \"Smith\"},{\"firstName\": \"Peter\",\"lastName\": \"Jones\"}]}";

var dictCategoryList = JsonConvert.DeserializeObject<Dictionary<string, List<FullName>>>(json);

where dictCategoryList is of type Dictionary<string, List<FullName>> .

In this way, you just have to use dictCategoryList.Count to detect the number of array objects in the JSON, and then access each one individually like below:

foreach (var category in dictCategoryList)
{
    foreach (var item in category.Value)
    {
        Console.WriteLine(string.Format("First Name: {0}, Last Name: {1}", item.firstName, item.lastName));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM