Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I am getting JSON response string, which is:

[{"Id":1,"Name":"name1"},{"Id":2,"Name":"name2"},{"Id":3,"Name":"name3"}]


now if I try to deserialize this string into class object and also in XmlDocument, its not working. Instead I am getting null value in both Class object and XmlDocument object.

What I have tried:

I tried the following for deserializing into a class

using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
  if (resp.StatusCode == HttpStatusCode.OK)
  {
    StreamReader rd = new StreamReader(resp.GetResponseStream());
    string str = rd.ReadToEnd(); //Here I can see the response JSON string
    Student std = JsonConvert.DeserializeObject<Student>(str);
  }
}


I tried the following for deserializing into XmlDocument

using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
  if (resp.StatusCode == HttpStatusCode.OK)
  {
    StreamReader rd = new StreamReader(resp.GetResponseStream());
    string str = rd.ReadToEnd(); //Here I can see the response JSON string
    XmlDocument xDoc = new XmlDocument();
    xDoc = JsonConvert.DeserializeXmlNode(str);
  }
}
Posted
Updated 24-Oct-17 19:05pm

Its a JSON array so parse it with Student Array
Student[] Students = JsonConvert.DeserializeObject<Student[]>(json);
            foreach (Student student in Students)
            {
                int id = student.Id;
                string name = student.Name;
                
            }


ensure the Student class looks like this
class Student
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
 
Share this answer
 
v3
To expand on Karthik Bangalore solution, Working with JSON in C# & VB[^] is a CodeProject article written for Q&A questions like this one. It covers tools, including code generation, helper classes, and full working samples that you can download and run.
 
Share this answer
 
Comments
Kingshuk_SP 25-Oct-17 2:42am    
Nice article Graeme :) Thanks and Cheers :)
Graeme_Grant 25-Oct-17 3:08am    
You are welcome. Too much to post here... Glad that it helped.
To further expand on Karthik Bangalore's solution, you dont need to use an array when List<t> is available so here's what I would do:

C#
using Newtonsoft.Json;
using System.Collections.Generic;

namespace WorkingWithJson
{
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Working_with_json
    {
        public static void DeserializeJSON()
        {
            // our json string
            string json = @"[{""Id"":1,""Name"":""name1""},{""Id"":2,""Name"":""name2""},{""Id"":3,""Name"":""name3""}]";
            
            // deserializing to a list of type Person
            List<Person> people = JsonConvert.DeserializeObject<List<Person>>(json);

            // which can then be retrieved thus
            System.Console.WriteLine($"{people[0].Id} - {people[0].Name}");
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900