skip to Main Content

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);

3

Answers


  1. 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);
    
    
    
    Login or Signup to reply.
  2. you could create a class with you json structure visual studio has one option
    for that

    enter image description here

    After with json net you can deserialize the message

    myObject jsonobj = JsonConvert.DeserializeObject<myObject>(json);
    
    Login or Signup to reply.
  3. 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("rnName: " + prop.Name + "  Value: " + prop.Value);
            else
            {
                Console.WriteLine("rnName: " + 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search