如何使用foreach循环读取JSON数据?C# ASP.NET

huangapple go评论99阅读模式
英文:

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&lt;Education&gt; 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&lt;Base&gt;(json);


答案3

得分: 1

你可以创建一个具有你的 JSON 结构的类,Visual Studio 有一个选项
用于此。

使用 JSON.NET,你可以反序列化消息。

myObject jsonobj = JsonConvert.DeserializeObject<myObject>(json);
英文:

you could create a class with you json structure visual studio has one option
for that

如何使用foreach循环读取JSON数据?C# ASP.NET

After with json net you can deserialize the message

myObject jsonobj = JsonConvert.DeserializeObject&lt;myObject&gt;(json);

huangapple
  • 本文由 发表于 2023年8月4日 00:34:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定