Afraz Ali
Afraz Ali

Reputation: 2752

How to parse JSON?

I am calling an api which return response in JSON format and I can't control its response.

The response that it is generating is similar to the following

{
   "success": "Yes",
   "resource": "Job/record",
   "count": 2,
   "last-modified": "2013-06-20 10:21:53",
   "Job": {
      "1297585": {
         "link": {},
         "last-modified": "2013-06-20 10:21:53",
         "id": "1297585"
      },
      "1319244": {
         "link": {},
         "last-modified": "2013-06-20 10:21:53",
         "id": "1319244"
      }
   }
}

I am using HttpWebRequest and HttpWebResponse to get this JSON. I need to de-serialize it to a POCO and have two problems.

  1. The response contains a last-modified property How do I convert it to a LastModified property in C# class.
  2. Secondly and most importantly the job array contains a dynamic property with id. How do I convert a dynamic value such as 1319244 to an ID property in a nested object.

I want to get a poco similar to the one below.

public class Response
    {
        public String Success { get; set; }
        public String Resource { get; set; }
        public Int32 Count { get; set; }
        public DateTime LastModified { get; set; }
        public Job[] Jobs { get; set; }
    }

    public class Job
    {
        public Int32 ID { get; set; }
        public String Link { get; set; }
        public String LastModified { get; set; }
    }

Do I need to parse this response manually or is there any easy way?

Upvotes: 2

Views: 255

Answers (2)

Afraz Ali
Afraz Ali

Reputation: 2752

I ended up using Json.Net and deserialize the Json manually. Turns out its not a big deal.

Here is the code

var jo = JObject.Parse(json);
        var data = (JObject)jo["Job"];
        foreach (var item in data)
        {
            JToken token = JToken.Parse(item.Value.ToString());

            Console.WriteLine(token.Value<String>("id"));
        }

All I had to do was to read the job object and then iterate it using a loop. Inside the loop I can read any property and assign it to my POCO.

Upvotes: 0

Brian Rogers
Brian Rogers

Reputation: 129707

  1. You can associate differently-named JSON and C# properties by decorating your C# properties with JsonProperty attributes specifying the name used in the JSON.

  2. If you have an object that contains dynamic property names, you need to use a Dictionary<string,X> to receive the deserialized values, where X is either object or a class you defined (I would recommend the latter wherever possible).

With these points in mind, you can define your classes like this:

public class Response
{
    [JsonProperty(PropertyName = "success")]
    public String Success { get; set; }

    [JsonProperty(PropertyName = "resource")]
    public String Resource { get; set; }

    [JsonProperty(PropertyName = "count")]
    public Int32 Count { get; set; }

    [JsonProperty(PropertyName="last-modified")]
    public DateTime LastModified { get; set; }

    [JsonProperty(PropertyName = "Job")]
    public Dictionary<string, Job> Jobs { get; set; }
}

public class Job
{
    [JsonProperty(PropertyName = "id")]
    public Int32 ID { get; set; }

    [JsonProperty(PropertyName = "link")]
    public object Link { get; set; }

    [JsonProperty(PropertyName = "last-modified")]
    public String LastModified { get; set; }
}

And you can deserialize into them like this:

Response responseObj = JsonConvert.DeserializeObject<Response>(jsonString);

Upvotes: 1

Related Questions