如何使用for each循环读取json数据?C#ASP.NET

xe55xuns  于 2023-08-08  发布在  C#
关注(0)|答案(3)|浏览(108)

如何在C#ASP.NET中使用for each循环读取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"
        }
      }
    ]
  }
}

个字符

vom3gejh

vom3gejh1#

你可以试试这个

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

ztyzrc3y

ztyzrc3y2#

创建一个具有相关属性的类,并将该类型传递给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);

字符串

rnmwe5a2

rnmwe5a23#

你可以用你的json结构创建一个类visual studio有一个选项


的数据
在使用jsonnet之后,您可以反序列化消息

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

字符串

相关问题