Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to loop the json object inside json object in c sharp

I have the following json array

var jsondataset = "{'tablename':'Employee','data':[{ 'Employee Name':'John','Salary':5000,'Department':'Marketing'},{ 'Employee Name':'Smith','Salary':4000,'Department':'IT'},{ 'Employee Name':'Williams','Salary':6000,'Department':'Sales'},{'Employee Name':'Vijay','Salary':6500,'Department':'IT'}],'Student':'Employee','data':[{'StudentID':1,'Name':'Suresh','Marks':950,'Grade':'A+'},{'StudentID':1,'Name':'Rama','Marks':900,'Grade':'A+'},{'StudentID':1,'Name':'Kishore','Marks':750,'Grade':'B'}]}";

I am looping on jsondataset as follows:

 JavaScriptSerializer json_serializer = new JavaScriptSerializer();
 var routes_list = (Dictionary<string, object>)json_serializer.DeserializeObject(jsondataset);
 string tname="";
 foreach (var record in routes_list)
 {
 }

But I don't understand how to loop on each record in the collection with key 'data'

Can you give to me a solution please?

like image 756
rakshithrai Avatar asked Jan 27 '23 21:01

rakshithrai


2 Answers

Taking into account the structure of your JSON you could deserialize it as follows:

var jsondataset = "{'tablename':'Employee','data':[{ 'Employee Name':'John','Salary':5000,'Department':'Marketing'},{ 'Employee Name':'Smith','Salary':4000,'Department':'IT'},{ 'Employee Name':'Williams','Salary':6000,'Department':'Sales'},{'Employee Name':'Vijay','Salary':6500,'Department':'IT'}],'Student':'Employee','data':[{'StudentID':1,'Name':'Suresh','Marks':950,'Grade':'A+'},{'StudentID':1,'Name':'Rama','Marks':900,'Grade':'A+'},{'StudentID':1,'Name':'Kishore','Marks':750,'Grade':'B'}]}";

JavaScriptSerializer json_serializer = new JavaScriptSerializer();
var routes_list = (Dictionary<string, object>)json_serializer.DeserializeObject(jsondataset);

foreach (var entry in routes_list)
{
    if (entry.Key == "data")
    {
        var objArr = (object[])entry.Value;
        foreach (var obj in objArr)
        {
            foreach (var item in (Dictionary<string, object>)obj)
            {
                Console.WriteLine(item.Key + ": " + item.Value);
            }
        }
    }
}

Output:

StudentID: 1 Name: Suresh Marks: 950 Grade: A+ StudentID: 1 Name: Rama Marks: 900 Grade: A+ StudentID: 1 Name: Kishore Marks: 750 Grade: B

like image 145
L_J Avatar answered Jan 30 '23 14:01

L_J


The simplest what you could do is use dynamic instead of var.

var jsonString = @"{'tablename':'Employee','data':[{ 'Employee Name':'John','Salary':5000,'Department':'Marketing'},{ 'Employee Name':'Smith','Salary':4000,'Department':'IT'},{ 'Employee Name':'Williams','Salary':6000,'Department':'Sales'},{'Employee Name':'Vijay','Salary':6500,'Department':'IT'}],'Student':'Employee','data':[{'StudentID':1,'Name':'Suresh','Marks':950,'Grade':'A+'},{'StudentID':1,'Name':'Rama','Marks':900,'Grade':'A+'},{'StudentID':1,'Name':'Kishore','Marks':750,'Grade':'B'}]}";
dynamic routes_list = json_serializer.DeserializeObject(jsondataset);
Console.WriteLine(routes_list["tablename"]);

But it would be better if you create a class containing the structure for the objects you want to deserialize. Doing this, makes accessing the data even easier. Assuming you have an error in your json, which I changed, it would look a bit like this:

class TableData
{
    public string TableName;
    public object[] Data;
}

class Employee
{
    public string EmployeeName;
    public int Salary;
    public string Department;
}

class Student
{
    public int StudentID;
    public string Name;
    public int Marks;
    public string Grade;
}

static void Main(string[] args)
{
    var jsondataset = @"
        [
            {
                'TableName': 'Employee',
                'Data': [
                    {
                        'EmployeeName': 'John',
                        'Salary': 5000,
                        'Department': 'Marketing'
                    },
                    {
                        'EmployeeName': 'Smith',
                        'Salary': 4000,
                        'Department': 'IT'
                    },
                    {
                        'EmployeeName': 'Williams',
                        'Salary': 6000,
                        'Department': 'Sales'
                    },
                    {
                        'EmployeeName': 'Vijay',
                        'Salary': 6500,
                        'Department': 'IT'
                    }
                ]
            },
            {
                'TableName': 'Student',
                'Data': [
                    {
                        'StudentID': 1,
                        'Name': 'Suresh',
                        'Marks': 950,
                        'Grade': 'A+'
                    },
                    {
                        'StudentID': 1,
                        'Name': 'Rama',
                        'Marks': 900,
                        'Grade': 'A+'
                    },
                    {
                        'StudentID': 1,
                        'Name': 'Kishore',
                        'Marks': 750,
                        'Grade': 'B'
                    }
                ]
            }
        ]
    ";

    JavaScriptSerializer json_serializer = new JavaScriptSerializer();
    var datasets = json_serializer.Deserialize<TableData[]>(jsonString);
    foreach (var dataset in datasets)
    {
        switch (dataset.TableName)
        {
            case "Student":
                foreach (var person in dataset.Data)
                {
                    var student = json_serializer.ConvertToType<Student>(person);
                    Console.WriteLine("Student " + student.Name + " has grade " + student.Grade);
                }
                break;
            case "Employee":
                foreach (var person in dataset.Data)
                {
                    var employee = json_serializer.ConvertToType<Employee>(person);
                    Console.WriteLine("Employee " + employee.EmployeeName + " has grade " + employee.Salary);
                }
                break;
            default:
                Console.WriteLine("Unknown datatable");
                break;
        }

    }
}
like image 26
nl-x Avatar answered Jan 30 '23 14:01

nl-x