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

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

How to read json data using for each loop? C# ASP.NET

问题

在C# ASP.NET中如何使用foreach循环读取json数据。我有以下json输出数据,我想使用foreach循环读取red高亮显示的education数据。以下是我的json数据。

  1. {
  2. "data": {
  3. "certifications": [],
  4. "dateOfBirth": null,
  5. "education": [
  6. {
  7. "id": 29364744,
  8. "organization": "Texas A&M University",
  9. "accreditation": {
  10. "education": "Bachelor of Science",
  11. "educationLevel": "bachelors",
  12. "inputStr": "Bachelor of Science Computer Science",
  13. "matchStr": ""
  14. },
  15. "grade": null,
  16. "location": {
  17. "formatted": "College Station, TX, USA",
  18. "streetNumber": null,
  19. "street": null,
  20. "apartmentNumber": null,
  21. "city": "College Station",
  22. "postalCode": null,
  23. "state": "Texas",
  24. "country": "United States",
  25. "rawInput": "College Station, TX",
  26. "countryCode": "US",
  27. "latitude": 30.627977,
  28. "longitude": -96.3344068
  29. },
  30. "dates": {
  31. "startDate": "2005-01-01",
  32. "completionDate": "2009-01-01",
  33. "isCurrent": false,
  34. "rawText": "2005 - 2009"
  35. }
  36. }
  37. ]
  38. }
  39. }
  1. var response = client.Post(request);
  2. var json = response.Content.ToString();
  3. 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.

  1. {
  2. "data": {
  3. "certifications": [],
  4. "dateOfBirth": null,
  5. "education": [
  6. {
  7. "id": 29364744,
  8. "organization": "Texas A&M University",
  9. "accreditation": {
  10. "education": "Bachelor of Science",
  11. "educationLevel": "bachelors",
  12. "inputStr": "Bachelor of Science Computer Science",
  13. "matchStr": ""
  14. },
  15. "grade": null,
  16. "location": {
  17. "formatted": "College Station, TX, USA",
  18. "streetNumber": null,
  19. "street": null,
  20. "apartmentNumber": null,
  21. "city": "College Station",
  22. "postalCode": null,
  23. "state": "Texas",
  24. "country": "United States",
  25. "rawInput": "College Station, TX",
  26. "countryCode": "US",
  27. "latitude": 30.627977,
  28. "longitude": -96.3344068
  29. },
  30. "dates": {
  31. "startDate": "2005-01-01",
  32. "completionDate": "2009-01-01",
  33. "isCurrent": false,
  34. "rawText": "2005 - 2009"
  35. }
  36. }
  37. ]
  38. }
  39. }
  1. var response = client.Post(request);
  2. var json = response.Content.ToString();
  3. var data = (JObject)JsonConvert.DeserializeObject(json);

答案1

得分: 2

以下是翻译好的部分:

  1. 你可以尝试类似这样的代码:
  1. JObject education = (JObject)JObject.Parse(json)["data"]["education"][0];
  2. foreach (var prop in education.Properties())
  3. {
  4. if (prop.Value.Type != JTokenType.Object) Console.WriteLine("\r\nName: " + prop.Name + " Value: " + prop.Value);
  5. else
  6. {
  7. Console.WriteLine("\r\nName: " + prop.Name + " Values:");
  8. foreach (var p in ((JObject)prop.Value).Properties())
  9. {
  10. Console.WriteLine(p.Name + " -- " + p.Value);
  11. }
  12. }
  13. }
  1. 输出结果:
  1. Name: id Value: 29364744
  2. Name: organization Value: Texas A&M University
  3. Name: accreditation Values:
  4. education -- Bachelor of Science
  5. educationLevel -- bachelors
  6. inputStr -- Bachelor of Science Computer Science
  7. matchStr --
  8. Name: grade Value:
  9. Name: location Values:
  10. formatted -- College Station, TX, USA
  11. streetNumber --
  12. street --
  13. apartmentNumber --
  14. city -- College Station
  15. postalCode --
  16. state -- Texas
  17. country -- United States
  18. rawInput -- College Station, TX
  19. countryCode -- US
  20. latitude -- 30.627977
  21. longitude -- -96.3344068
  22. Name: dates Values:
  23. startDate -- 2005-01-01
  24. completionDate -- 2009-01-01
  25. isCurrent -- False
  26. rawText -- 2005 - 2009
英文:

you can try something like this

  1. JObject education = (JObject)JObject.Parse(json)["data"]["education"][0];
  2. foreach (var prop in education.Properties())
  3. {
  4. if (prop.Value.Type != JTokenType.Object) Console.WriteLine("\r\nName: " + prop.Name + " Value: " + prop.Value);
  5. else
  6. {
  7. Console.WriteLine("\r\nName: " + prop.Name + " Values:");
  8. foreach (var p in ((JObject)prop.Value).Properties())
  9. {
  10. Console.WriteLine(p.Name + " -- " + p.Value);
  11. }
  12. }
  13. }

output

  1. Name: id Value: 29364744
  2. Name: organization Value: Texas A&M University
  3. Name: accreditation Values:
  4. education -- Bachelor of Science
  5. educationLevel -- bachelors
  6. inputStr -- Bachelor of Science Computer Science
  7. matchStr --
  8. Name: grade Value:
  9. Name: location Values:
  10. formatted -- College Station, TX, USA
  11. streetNumber --
  12. street --
  13. apartmentNumber --
  14. city -- College Station
  15. postalCode --
  16. state -- Texas
  17. country -- United States
  18. rawInput -- College Station, TX
  19. countryCode -- US
  20. latitude -- 30.627977
  21. longitude -- -96.3344068
  22. Name: dates Values:
  23. startDate -- 2005-01-01
  24. completionDate -- 2009-01-01
  25. isCurrent -- False
  26. rawText -- 2005 - 2009

答案2

得分: 1

创建一个具有相关属性的类,并将该类型传递给DeserializeObject。

  1. public class Education
  2. {
  3. public string id { get; set; }
  4. public string organization { get; set; }
  5. }
  6. public class Employee
  7. {
  8. public List<Education> education { get; set; }
  9. }
  10. public class Base
  11. {
  12. public Employee data { get; set; }
  13. }
  14. var response = client.Post(request);
  15. var json = response.Content.ToString();
  16. var data = JsonConvert.DeserializeObject<Base>(json);
英文:

Create a classes with the relevant properties and pass that type to DeserializeObject.

  1. public class Education
  2. {
  3. public string id { get; set; }
  4. public string organization { get; set; }
  5. }
  6. public class Employee
  7. {
  8. public List&lt;Education&gt; education { get; set; }
  9. }
  10. public class Base
  11. {
  12. public Employee data { get; set; }
  13. }
  14. var response = client.Post(request);
  15. var json = response.Content.ToString();
  16. var data = JsonConvert.DeserializeObject&lt;Base&gt;(json);

答案3

得分: 1

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

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

  1. 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

  1. 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:

确定