在 c# 关系中使用 json.net 反序列化 Json var
本文关键字:net 反序列化 Json var json 关系 | 更新日期: 2023-09-27 18:32:30
我有一个json字符串,我试图在其中为EmployeeId,EmployeeName和日期制作一个foreach:
{"$id":"1","Employee":{"$id":"2","RelationshipManager":{"$id":"3","_owner":{"$ref":"2"},"_relationships":[[{"$id":"4","Calendarbookings":null,"Id":1,"MId":1,"Startdate":"2011-01-14T00:00:00","Enddate":"2011-01-14T00:00:00","StartTime":"1899-12-30T10:00:00","Endtime":"1899-12-30T16:00:00","EmployeeId":1,"DebtorId":0,"Subject":"Tidevand Odense","Description":null,"AllDay":0,"Wwwaddress":"","Mailaddress":"","IsTeam":1,"IsRessource":0,"RessourceType":0,"Participants":1,"Department":0,"IsRepeating":0,"TaskType":0,"Categorytype":0,"Price":495.0,"WaitinglistIsEnabled":1,"Deadline":"2011-02-01T00:00:00","DeadlineForCancellation":"1899-12-30T00:00:00","Ending":"2011-01-14T00:00:00","OnlineEnabled":1,"Revenueaccount":1502,"Maxbooking":20,"Employee":{"$ref":"2"}}]]},"Id":1,"SearchKey":"","EmployeeName":"Erik Steen Larsen","Address1":"","Address2":"","Zip":"","City":"","Phone":"","CashSuper":"0","ActiveInCalendar":"1","Startdate":"1899-12-30T00:00:00","Enddate":"1899-12-30T00:00:00","Mobile":"30125744","Description":null,"Events":[{"$ref":"4"}]},"ErrorCode":0,"Status":0,"StatusString":"Okay","Message":null,"ApiVersion":"1.0.0.8"}
怎么做呢???
_jsonContent = JsonConvert.DeserializeObject(JSONSTRING);
foreach (var item in _jsonContent.Employee)
{
littext.Text += item.EmployeeName;
littext.Text += item.Id;
}
谢谢
您可以使用动态对象来实现您想要的东西,如下所示:
dynamic astring = "{'"$id'":'"1'",'"Employee'":{'"$id'":'"2'",'"RelationshipManager'":{'"$id'":'"3'",'"_owner'":{'"$ref'":'"2'"},'"_relationships'":[[{'"$id'":'"4'",'"Calendarbookings'":null,'"Id'":1,'"MId'":1,'"Startdate'":'"2011-01-14T00:00:00'",'"Enddate'":'"2011-01-14T00:00:00'",'"StartTime'":'"1899-12-30T10:00:00'",'"Endtime'":'"1899-12-30T16:00:00'",'"EmployeeId'":1,'"DebtorId'":0,'"Subject'":'"Tidevand Odense'",'"Description'":null,'"AllDay'":0,'"Wwwaddress'":'"'",'"Mailaddress'":'"'",'"IsTeam'":1,'"IsRessource'":0,'"RessourceType'":0,'"Participants'":1,'"Department'":0,'"IsRepeating'":0,'"TaskType'":0,'"Categorytype'":0,'"Price'":495.0,'"WaitinglistIsEnabled'":1,'"Deadline'":'"2011-02-01T00:00:00'",'"DeadlineForCancellation'":'"1899-12-30T00:00:00'",'"Ending'":'"2011-01-14T00:00:00'",'"OnlineEnabled'":1,'"Revenueaccount'":1502,'"Maxbooking'":20,'"Employee'":{'"$ref'":'"2'"}}]]},'"Id'":1,'"SearchKey'":'"'",'"EmployeeName'":'"Erik Steen Larsen'",'"Address1'":'"'",'"Address2'":'"'",'"Zip'":'"'",'"City'":'"'",'"Phone'":'"'",'"CashSuper'":'"0'",'"ActiveInCalendar'":'"1'",'"Startdate'":'"1899-12-30T00:00:00'",'"Enddate'":'"1899-12-30T00:00:00'",'"Mobile'":'"30125744'",'"Description'":null,'"Events'":[{'"$ref'":'"4'"}]},'"ErrorCode'":0,'"Status'":0,'"StatusString'":'"Okay'",'"Message'":null,'"ApiVersion'":'"1.0.0.8'"}";
dynamic jsonContent = JsonConvert.DeserializeObject(astring);
foreach (var item in jsonContent.Employee)
{
/*.,..*/
}
你也可以使用匿名对象
var template = new { EmployeName = "", EmplyeeId = 0 };
var jsonContent = JsonConvert.DeserializeAnonymousType(JSONSTRING, template);
foreach (var item in jsonContent.Employee)
{
/*.,..*/
}
JSON 中的Employee
不是一个数组。还需要将 json 反序列化到适当的容器中。
乔布特:
var jObject = JsonConvert.DeserializeObject<JObject>(json);
var employeeId = (int) jObject["Employee"]["Id"];
var employeeName = (string) jObject["Employee"]["EmployeeName"];
动态:
var jObject = JsonConvert.DeserializeObject<dynamic>(json);
var employeeId = (int) jObject.Employee.Id;
var employeeName = (string) jObject.Employee.EmployeeName;
波科:
public class Record
{
[JsonProperty("Employee")]
public Employee Employee { get; set; }
}
public class Employee
{
[JsonProperty("Id")]
public int Id { get; set; }
[JsonProperty("EmployeeName")]
public string EmployeeName { get; set; }
}
var record = JsonConvert.DeserializeObject<Record>(json);
var employeeId = record.Employee.Id;
var employeeName = record.Employee.EmployeeName;