Deserialise嵌套JSON与Newtonsoft和C# [英] Deserialise nested JSON with Newtonsoft and C#
问题描述
我试图解析JSON响应形成一个REST API。我能得到的反应很好,也创造了一些中高级车型。我使用Newtonsoft的Json.Net。
I am trying to parse a Json response form a Rest API. I can get the response fine and have created some class Models. I am using Newtonsoft's Json.Net.
我保持我的回应得到空值,我不知道我是否有设置我的模型正确,或者我失去了一些东西?
I keep getting null values in my response and am not sure if I have setup my Models correct or am missing something?
我想获得的,例如,名字和ReferenceNumber这是在数据\\项目\\员工
What I am trying to get is for example, Firstname and ReferenceNumber which are in Data\Items\Employee
我的JSON响应的一个例子是:
An example of my Json response is:
{
"Data": {
"Links": [
{
"Rel": "parent",
"Href": "http://Api/url/",
"Title": "Api",
}
],
"Items": [
{
"Employee": {
"ReferenceNumber": "0078952",
"EmployeeStatus": {
"Name": "Active",
"Value": 2
},
"ContactByPost": true,
"ContactByMarketingEmails": true,
"ClientDetails": [
{
"LimitedCompany": {
"Id": "809f4455-673d-df11-9849-00155d072900",
"Name": "Company"
},
"ClientCompany": {
"Id": "62494f0c-617d-412f-81a3-ca40ef80f774",
"Name": "Company 2"
},
"Code": "G67D",
"RefCode": "1271",
"Date": "\/Date(1333456265000+0100)\/",
"Id": "009ea227-887d-e111-96ec-000c29128cee",
"CreatedOn": "\/Date(1333455954000+0100)\/",
"CreatedBy": {
"Id": "5c2cb5eb-7368-e111-96eb-000c29128cee",
"Name": "System Administrator"
},
"Archived": false
}
],
"Title": {
"Name": "Mr",
"Value": 1
},
"FirstName": "Fred",
"MiddleName": null,
"LastName": "Flintstone",
"KnownAs": null,
"DateOfBirth": "\/Date(315576000000+0000)\/",
"Gender": {
"Name": "Male",
"Value": 1
},
"HomePhoneNumber": "55555555555",
"MobilePhoneNumber": null,
"Addresses": null,
"EmailAddresses": null,
"Id": "009ea227-887d-e111-96ec-000c29128cee",
"CreatedOn": "\/Date(1333455954000+0100)\/",
"CreatedBy": {
"Id": "5c2cb5eb-7368-e111-96eb-000c29128cee",
"Name": "System Administrator"
},
"Archived": false
},
"Links": [
{
"Rel": "list",
"Href": "http://Api/Addresses",
"Title": "Addresses",
"Description": "Get a list of addresses for this Employee."
}
]
}
]
}
}
我的模型都设置如下图所示:
My Models are setup as shown below:
public class Data
{
public Collection<Links> Links { get; set; }
public Collection<Items> Items { get; set; }
}
public class Items
{
public Employee Employee { get; set; }
}
public class Employee
{
public string ReferenceNumber { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Id { get; set; }
}
public class Links
{
public string Rel { get; set; }
public string Href { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
在我的控制器我做以下内容:
In my Controller I am doing the following:
Data data = Newtonsoft.Json.JsonConvert.DeserializeObject<Data>(APIResponse);
我看到的调试时项目和这两者都是空链接。我真的不知道我在做什么错误或如何得到这个工作,所以我可以在我的C#code访问值?任何帮助将是AP preciated。该应用程序是MVC3。这些例子我见过使用简单的JSON结构。
All I see when debugging is Items and Links which are both null. I'm not really sure what I am doing wrong or how I get this to work so I can access the values in my C# code? Any help would be appreciated. The application is MVC3. The examples I've seen are using simple json structures.
推荐答案
我设法解决这个问题。我所做的就是先创建父类来处理数据对象,@Jake也提及。我创建了一个类类型ClientDetails我曾在Employee类的如下:
I managed to fix this. What i did was to first create the parent class to deal with the Data object, as @Jake had also mentioned. I created a class of type ClientDetails which I had in the Employee class as follows:
public ClientDetails clientDetails {get; set;}
这导致了错误的ClientDetails是一个数组。我改变了这种为以下内容:
This caused an error as ClientDetails is an array. I changed this to the following:
public List<ClientDetails> clientDetails {get; set;}
这解决了这个问题,现在我的整个数据对象是填充了API的响应。
This fixed the problem and now my whole Data object is populated with the response from the API.
@Jake,感谢您的输入...
@Jake, thanks for your input...
这篇关于Deserialise嵌套JSON与Newtonsoft和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!