读取 JSON 时出错无法访问 Newtonsoft.Json.Linq.JProperty 上的子值
Error Cannot access child value on Newtonsoft.Json.Linq.JProperty while reading JSON
我正在尝试从其余响应对象中读取属性。响应是一个 json 对象数组,如下所示。我想读取属性 lastName, titles.investigatorTitle.name, CurrentMembership.currentMembershipType.name
{
"data": [
{
"investigatorID": 3,
"firstName": "JH",
"middleName": "JH",
"lastName": "HV",
"titles": [
{
"investigatorTitle": {
"id": 7,
"name": "Professor",
"active": true
},
"additionalDetails": null,
"startDate": null,
"endDate": null,
"displayIndex": 1
}
],
"CurrentMembership": [
{
"investigatorCenterMembershipID": 4,
"investigatorCenter": {
"id": 1,
"name": "SCG"
},
"currentMembershipType": {
"id": 11,
"name": "ABC Member",
"code": "ABC",
"active": true
}
}
]
},
{
"investigatorID": 4,
"firstName": "FG",
"middleName": "JH",
"lastName": "VF",
"titles": [
{
"investigatorTitle": {
"id": 7,
"name": "Professor",
"active": true
},
"additionalDetails": null,
"startDate": null,
"endDate": null,
"displayIndex": 1
}
],
"CurrentMembership": [
{
"investigatorCenterMembershipID": 4,
"investigatorCenter": {
"id": 1,
"name": "SCG"
},
"currentMembershipType": {
"id": 11,
"name": "ABC Member",
"code": "ABC",
"active": true
}
}
]
}
],
"meta": {
"totalRecords": 675,
"totalPages": 27
},
"links": null
}
我收到错误 System.InvalidOperationException: 'Cannot access child value on Newtonsoft.Json.Linq.JProperty.' at c.Name = i[0].lastName;
我怎样才能正确访问它?
IRestResponse response = client.Execute(request);
var InvestigatorList = new List<searchResults>();
dynamic jsonResponse = JsonConvert.DeserializeObject(response.Content);
foreach (dynamic i in jsonResponse)
{
Contact c = new Contact();
c.Name = i[0].lastName; //THE ERROR IS HAPPENING HERE
InvestigatorList.Add(new searchResults()
{
Name = c.Name,
Contact = c
});
}
好的,我不确定你在做什么,因为这是我不熟悉的方法。
我对 newtonsoft 所做的是构建 类 以反映 Json 格式。然后反序列化到它上面。
Class:
public class DeserializeJson
{
public DataList ExtractedData;
public DeserializeJson(IRestResponse response)
{
try
{
ExtractedData = JsonConvert.DeserializeObject<DataList>(response.Content);
}
catch(Exception ex)
{
throw new Exception(ex.Message());
}
}
public class DataList
{
public List<Data> Data {get; set;}
public meta meta {get; set;}
public string? links {get;set;}
}
public class Data
{
public int investigatorID {get; set;}
public string firstName {get; set;}
public string middleName {get; set;}
public string lastName {get; set;}
public List<titles> titles {get; set;}
public List<CurrentMembership> {get; set;}
}
public class meta
{
public int totalRecords {get; set;}
public int totalPages {get; set;}
}
public class CurrentMembership
{
public int investigatorCenterMembershipID {get; set;}
public investigatorCenter investigatorCenter {get; set;}
public currentMembershipType currentMembershipType {get; set;}
}
public class titles
{
public int investigatorID {get; set;}
//not sure what additionalDetails could also hold but you have to reflect it
public bool additionalDetails {get; set;}
public DateTime? startDate {get; set;}
public DateTime? endDate {get; set;}
public int displayIndex {get; set;}
public investigatorTitle investigatorTitle{get;set;}
}
public class investigatorTitle
{
public int id {get; set;}
public string name {get; set;}
public bool active {get; set;}
}
public class currentMembershipType
{
public int id {get; set;}
public string name {get; set;}
}
public class investigatorCenter
{
public int id {get; set;}
public string name {get; set;}
public string code {get; set;}
public bool active {get; set;}
}
}
在那之后我会传递你想要的任何东西,因为那里的一切都可以通过 linq 访问。
例如:
访问:
//deserialize
DeserializeJson jsonResponse = new DeserializeJson(response);
//access excample
List<string> InvestigatorList = jsonResponse.ExtractedData.Data.Select(name=>name.lastName).ToList();
试试这个,它在 Visual Studio
中测试过
var response = client.Execute(request);
var data = JObject.Parse(response.Content)["data"];
List<SearchResults> investigatorList = data.Select(d => new SearchResults {
Name = (string)d["lastName"], Contact = new Contact {Name = (string)d["lastName"]}}).ToList();
结果(序列化为json格式)
[
{
"Name": "HV",
"Contact": {
"Name": "HV"
}
},
{
"Name": "VF",
"Contact": {
"Name": "VF"
}
}
]
类
public class Contact
{
public string Name { get; set; }
}
public class SearchResults
{
public string Name { get; set; }
public Contact Contact { get; set; }
}
我正在尝试从其余响应对象中读取属性。响应是一个 json 对象数组,如下所示。我想读取属性 lastName, titles.investigatorTitle.name, CurrentMembership.currentMembershipType.name
{
"data": [
{
"investigatorID": 3,
"firstName": "JH",
"middleName": "JH",
"lastName": "HV",
"titles": [
{
"investigatorTitle": {
"id": 7,
"name": "Professor",
"active": true
},
"additionalDetails": null,
"startDate": null,
"endDate": null,
"displayIndex": 1
}
],
"CurrentMembership": [
{
"investigatorCenterMembershipID": 4,
"investigatorCenter": {
"id": 1,
"name": "SCG"
},
"currentMembershipType": {
"id": 11,
"name": "ABC Member",
"code": "ABC",
"active": true
}
}
]
},
{
"investigatorID": 4,
"firstName": "FG",
"middleName": "JH",
"lastName": "VF",
"titles": [
{
"investigatorTitle": {
"id": 7,
"name": "Professor",
"active": true
},
"additionalDetails": null,
"startDate": null,
"endDate": null,
"displayIndex": 1
}
],
"CurrentMembership": [
{
"investigatorCenterMembershipID": 4,
"investigatorCenter": {
"id": 1,
"name": "SCG"
},
"currentMembershipType": {
"id": 11,
"name": "ABC Member",
"code": "ABC",
"active": true
}
}
]
}
],
"meta": {
"totalRecords": 675,
"totalPages": 27
},
"links": null
}
我收到错误 System.InvalidOperationException: 'Cannot access child value on Newtonsoft.Json.Linq.JProperty.' at c.Name = i[0].lastName;
我怎样才能正确访问它?
IRestResponse response = client.Execute(request);
var InvestigatorList = new List<searchResults>();
dynamic jsonResponse = JsonConvert.DeserializeObject(response.Content);
foreach (dynamic i in jsonResponse)
{
Contact c = new Contact();
c.Name = i[0].lastName; //THE ERROR IS HAPPENING HERE
InvestigatorList.Add(new searchResults()
{
Name = c.Name,
Contact = c
});
}
好的,我不确定你在做什么,因为这是我不熟悉的方法。 我对 newtonsoft 所做的是构建 类 以反映 Json 格式。然后反序列化到它上面。
Class:
public class DeserializeJson
{
public DataList ExtractedData;
public DeserializeJson(IRestResponse response)
{
try
{
ExtractedData = JsonConvert.DeserializeObject<DataList>(response.Content);
}
catch(Exception ex)
{
throw new Exception(ex.Message());
}
}
public class DataList
{
public List<Data> Data {get; set;}
public meta meta {get; set;}
public string? links {get;set;}
}
public class Data
{
public int investigatorID {get; set;}
public string firstName {get; set;}
public string middleName {get; set;}
public string lastName {get; set;}
public List<titles> titles {get; set;}
public List<CurrentMembership> {get; set;}
}
public class meta
{
public int totalRecords {get; set;}
public int totalPages {get; set;}
}
public class CurrentMembership
{
public int investigatorCenterMembershipID {get; set;}
public investigatorCenter investigatorCenter {get; set;}
public currentMembershipType currentMembershipType {get; set;}
}
public class titles
{
public int investigatorID {get; set;}
//not sure what additionalDetails could also hold but you have to reflect it
public bool additionalDetails {get; set;}
public DateTime? startDate {get; set;}
public DateTime? endDate {get; set;}
public int displayIndex {get; set;}
public investigatorTitle investigatorTitle{get;set;}
}
public class investigatorTitle
{
public int id {get; set;}
public string name {get; set;}
public bool active {get; set;}
}
public class currentMembershipType
{
public int id {get; set;}
public string name {get; set;}
}
public class investigatorCenter
{
public int id {get; set;}
public string name {get; set;}
public string code {get; set;}
public bool active {get; set;}
}
}
在那之后我会传递你想要的任何东西,因为那里的一切都可以通过 linq 访问。
例如:
访问:
//deserialize
DeserializeJson jsonResponse = new DeserializeJson(response);
//access excample
List<string> InvestigatorList = jsonResponse.ExtractedData.Data.Select(name=>name.lastName).ToList();
试试这个,它在 Visual Studio
中测试过var response = client.Execute(request);
var data = JObject.Parse(response.Content)["data"];
List<SearchResults> investigatorList = data.Select(d => new SearchResults {
Name = (string)d["lastName"], Contact = new Contact {Name = (string)d["lastName"]}}).ToList();
结果(序列化为json格式)
[
{
"Name": "HV",
"Contact": {
"Name": "HV"
}
},
{
"Name": "VF",
"Contact": {
"Name": "VF"
}
}
]
类
public class Contact
{
public string Name { get; set; }
}
public class SearchResults
{
public string Name { get; set; }
public Contact Contact { get; set; }
}