我有以下json数组
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'}]}";
我正在如下循环jsondataset
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
var routes_list = (Dictionary<string, object>)json_serializer.DeserializeObject(jsondataset);
string tname="";
foreach (var record in routes_list)
{
}
但我不了解如何循环键“数据”中的每个记录
请提供解决方案
答案 0 :(得分:5)
考虑到JSON的结构,您可以按以下方式反序列化它:
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);
}
}
}
}
输出:
StudentID: 1
Name: Suresh
Marks: 950
Grade: A+
StudentID: 1
Name: Rama
Marks: 900
Grade: A+
StudentID: 1
Name: Kishore
Marks: 750
Grade: B
答案 1 :(得分:3)
最简单的方法是使用dynamic
而不是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"]);
但是,如果您创建一个包含要反序列化的对象的结构的类,那会更好。这样做使访问数据更加容易。假设您在json中有一个错误(我已将其更改),则它看起来会像这样:
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;
}
}
}