英文:
How to read json data using for each loop? C# ASP.NET
问题
在C# ASP.NET中如何使用foreach
循环读取json
数据。我有以下json
输出数据,我想使用foreach
循环读取red
高亮显示的education
数据。以下是我的json
数据。
{
"data": {
"certifications": [],
"dateOfBirth": null,
"education": [
{
"id": 29364744,
"organization": "Texas A&M University",
"accreditation": {
"education": "Bachelor of Science",
"educationLevel": "bachelors",
"inputStr": "Bachelor of Science Computer Science",
"matchStr": ""
},
"grade": null,
"location": {
"formatted": "College Station, TX, USA",
"streetNumber": null,
"street": null,
"apartmentNumber": null,
"city": "College Station",
"postalCode": null,
"state": "Texas",
"country": "United States",
"rawInput": "College Station, TX",
"countryCode": "US",
"latitude": 30.627977,
"longitude": -96.3344068
},
"dates": {
"startDate": "2005-01-01",
"completionDate": "2009-01-01",
"isCurrent": false,
"rawText": "2005 - 2009"
}
}
]
}
}
var response = client.Post(request);
var json = response.Content.ToString();
var data = (JObject)JsonConvert.DeserializeObject(json);
请注意,我没有翻译代码部分,只翻译了文本内容。
英文:
How to read json
data using for each loop in C# ASP.NET. I have following output data of json
and i want to read red
highlighted data education
using foreach
loop.
Here is my json
data.
{
"data": {
"certifications": [],
"dateOfBirth": null,
"education": [
{
"id": 29364744,
"organization": "Texas A&M University",
"accreditation": {
"education": "Bachelor of Science",
"educationLevel": "bachelors",
"inputStr": "Bachelor of Science Computer Science",
"matchStr": ""
},
"grade": null,
"location": {
"formatted": "College Station, TX, USA",
"streetNumber": null,
"street": null,
"apartmentNumber": null,
"city": "College Station",
"postalCode": null,
"state": "Texas",
"country": "United States",
"rawInput": "College Station, TX",
"countryCode": "US",
"latitude": 30.627977,
"longitude": -96.3344068
},
"dates": {
"startDate": "2005-01-01",
"completionDate": "2009-01-01",
"isCurrent": false,
"rawText": "2005 - 2009"
}
}
]
}
}
var response = client.Post(request);
var json = response.Content.ToString();
var data = (JObject)JsonConvert.DeserializeObject(json);
答案1
得分: 2
以下是翻译好的部分:
你可以尝试类似这样的代码:
JObject education = (JObject)JObject.Parse(json)["data"]["education"][0];
foreach (var prop in education.Properties())
{
if (prop.Value.Type != JTokenType.Object) Console.WriteLine("\r\nName: " + prop.Name + " Value: " + prop.Value);
else
{
Console.WriteLine("\r\nName: " + prop.Name + " Values:");
foreach (var p in ((JObject)prop.Value).Properties())
{
Console.WriteLine(p.Name + " -- " + p.Value);
}
}
}
输出结果:
Name: id Value: 29364744
Name: organization Value: Texas A&M University
Name: accreditation Values:
education -- Bachelor of Science
educationLevel -- bachelors
inputStr -- Bachelor of Science Computer Science
matchStr --
Name: grade Value:
Name: location Values:
formatted -- College Station, TX, USA
streetNumber --
street --
apartmentNumber --
city -- College Station
postalCode --
state -- Texas
country -- United States
rawInput -- College Station, TX
countryCode -- US
latitude -- 30.627977
longitude -- -96.3344068
Name: dates Values:
startDate -- 2005-01-01
completionDate -- 2009-01-01
isCurrent -- False
rawText -- 2005 - 2009
英文:
you can try something like this
JObject education = (JObject)JObject.Parse(json)["data"]["education"][0];
foreach (var prop in education.Properties())
{
if (prop.Value.Type != JTokenType.Object) Console.WriteLine("\r\nName: " + prop.Name + " Value: " + prop.Value);
else
{
Console.WriteLine("\r\nName: " + prop.Name + " Values:");
foreach (var p in ((JObject)prop.Value).Properties())
{
Console.WriteLine(p.Name + " -- " + p.Value);
}
}
}
output
Name: id Value: 29364744
Name: organization Value: Texas A&M University
Name: accreditation Values:
education -- Bachelor of Science
educationLevel -- bachelors
inputStr -- Bachelor of Science Computer Science
matchStr --
Name: grade Value:
Name: location Values:
formatted -- College Station, TX, USA
streetNumber --
street --
apartmentNumber --
city -- College Station
postalCode --
state -- Texas
country -- United States
rawInput -- College Station, TX
countryCode -- US
latitude -- 30.627977
longitude -- -96.3344068
Name: dates Values:
startDate -- 2005-01-01
completionDate -- 2009-01-01
isCurrent -- False
rawText -- 2005 - 2009
答案2
得分: 1
创建一个具有相关属性的类,并将该类型传递给DeserializeObject。
public class Education
{
public string id { get; set; }
public string organization { get; set; }
}
public class Employee
{
public List<Education> education { get; set; }
}
public class Base
{
public Employee data { get; set; }
}
var response = client.Post(request);
var json = response.Content.ToString();
var data = JsonConvert.DeserializeObject<Base>(json);
英文:
Create a classes with the relevant properties and pass that type to DeserializeObject.
public class Education
{
public string id { get; set; }
public string organization { get; set; }
}
public class Employee
{
public List<Education> education { get; set; }
}
public class Base
{
public Employee data { get; set; }
}
var response = client.Post(request);
var json = response.Content.ToString();
var data = JsonConvert.DeserializeObject<Base>(json);
答案3
得分: 1
你可以创建一个具有你的 JSON 结构的类,Visual Studio 有一个选项
用于此。
使用 JSON.NET,你可以反序列化消息。
myObject jsonobj = JsonConvert.DeserializeObject<myObject>(json);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论