1

I have a json file.

{
"Enumerations" : {
        "IndianCities" : [
            { "key" : "Delhi", "val" : 001},
            { "key" : "Mumbai", "val" : 002},
            ],
            .
            .
           },
"Users":[
 {
        "AccessType" : "Admin",
        "Male" : [
            {
                "Name" : "Xyz",
                "ID" : 459,
            },
            {
                "Name" : "Abc",
                "ID" : 542,
            }
        ],      

        "Female" : [
          {
                "Name" : "Abc",
                "ID" : 543,
            }
        ]
    },
{
        "Location" : "NewYork",
        "Male" : [
            {
                "Name" : "Xyz",
                "ID" : 460,
            },
            {
                "Name" : "Abc",
                "ID" : 642,
            }
        ],      

        "Female" : [
          {
                "Name" : "Abc",
                "ID" : 643,
            }
        ]
    },
]

Here, I want to store all enum data(both key and val info) in a list. Also from users class, I want to store info of all male users from all nested classes. Here, I wrote two classes names("Admin" and "NewYork") but in my json I have around 100 classes. Is there any generic solution available for reading male info all classes and put in a list.

1 Answers1

1

Welcome to Stackoverflow!

In the future, please make sure if you are asking a question, the code you post has no errors or if it does have errors, then your question can be about fixing the error.

Your JSON is not valid JSON.

Step 1

Fix your JSON. Here is the corrected version of your JSON:

{
"Enumerations" : {
        "IndianCities" : [
            { "key" : "Delhi", "val" : 001},
            { "key" : "Mumbai", "val" : 002}
            ]
           },
"Users":[
 {
        "AccessType" : "Admin",
        "Male" : [
            {
                "Name" : "Xyz",
                "ID" : 459
            },
            {
                "Name" : "Abc",
                "ID" : 542
            }
        ],      

        "Female" : [
          {
                "Name" : "Abc",
                "ID" : 543
            }
        ]
    },
{
        "Location" : "NewYork",
        "Male" : [
            {
                "Name" : "Xyz",
                "ID" : 460
            },
            {
                "Name" : "Abc",
                "ID" : 642
            }
        ],      

        "Female" : [
          {
                "Name" : "Abc",
                "ID" : 643
            }
        ]
    }
]
}

Step 2

As I mentioned in my comment, refer the answer here to create C# classes to represent your JSON. I did it, and it generated this:

public class Rootobject
{
    public Enumerations Enumerations { get; set; }
    public User[] Users { get; set; }
}

public class Enumerations
{
    public Indiancity[] IndianCities { get; set; }
}

public class Indiancity
{
    public string key { get; set; }
    public int val { get; set; }
}

public class User
{
    public string AccessType { get; set; }
    public Male[] Male { get; set; }
    public Female[] Female { get; set; }
    public string Location { get; set; }
}

public class Male
{
    public string Name { get; set; }
    public int ID { get; set; }
}

public class Female
{
    public string Name { get; set; }
    public int ID { get; set; }
}

Step 3

Is there any generic solution available for reading male info all classes and put in a list.

Yes! But what you really mean is:

Is there any generic solution available for reading all males into a list?

Use Newtonsoft.Json library to deserialize the JSON content. Please note each User instance has a Male array so you need to use SelectMany to flatten the data as shown below:

Rootobject ro = JsonConvert.DeserializeObject<Rootobject>("YourJSON");
List<Male> males = ro.Users.SelectMany(x => x.Male).ToList();

You may do other filtration if you require.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64