J R B
J R B

Reputation: 2136

Unable to deserialize JSON in c#

I am getting the below JSON in response from a REST API.

{
   "data":{
      "id":123,
      "zoneid":"mydomain.com",
      "parent_id":null,
      "name":"jaz",
      "content":"172.1 6.15.235",
      "ttl":60,
      "priority":null,
      "type":"A",
      "regions":[
         "global"
      ],
      "system_record":false,
      "created_at":"2017-09-28T12:12:17Z",
      "updated_at":"2017-09-28T12:12:17Z"
   }
}

and trying to resolve using below code but that doesn't result in a correctly deserialized type.

var model = JsonConvert.DeserializeObject<ResponseModel>(response);           

below is a class according the field I received in JSON response.

 public class ResponseModel
{
    public int id { get; set; }
    public string zone_id { get; set; }
    public int parent_id { get; set; }
    public string name { get; set; }
    public string content { get; set; }
    public int ttl { get; set; }
    public int priority { get; set; }
    public string type { get; set; }
    public string[] regions { get; set; }
    public bool system_record { get; set; }
    public DateTime created_at { get; set; }
    public DateTime updated_at { get; set; }

}

What is missing?

Upvotes: 0

Views: 2458

Answers (4)

Nick
Nick

Reputation: 39

Here a cool trick you can do in Visual Studio 2015-2017 where it generates the the correct class if you just copy the JSON (ctrl + c).

You need to create a new class in visual studio and once inside the class go to Edit menu -> Paste special -> paste JSON As Classes.

Steps to generate json class

This will generate the C# object for that json for you and save you all the hassle :)

Upvotes: 2

rene
rene

Reputation: 42414

You're missing a wrapper class.

public class Wrapper 
{
   public ResponseModel data {get;set}
}

and then do:

var model = JsonConvert.DeserializeObject<Wrapper>(response).data; 

to get the instance of your ResponseModel out the data property.

You can deduct this from your json:

{ "data": 
   { "id":123, /*rest omitted */ }
}

The type that will receive this JSON needs to have a property named data. The suggested Wrapper class acts as that type.

Upvotes: 2

Jamiec
Jamiec

Reputation: 136074

Your model does not match your response - it matches the data property. Simply wrap another object round it

public class ResponseData
{
    public ResponseModel Data {get; set; {
}

and then

var model = JsonConvert.DeserializeObject<ResponseData>(response); 

Upvotes: 1

OrcusZ
OrcusZ

Reputation: 3660

According to json2csharp website, your model seems to be incorrect. Try this one :

public class ResponseModel
{
    public int id { get; set; }
    public string zoneid { get; set; }
    public object parent_id { get; set; }
    public string name { get; set; }
    public string content { get; set; }
    public int ttl { get; set; }
    public object priority { get; set; }
    public string type { get; set; }
    public List<string> regions { get; set; }
    public bool system_record { get; set; }
    public DateTime created_at { get; set; }
    public DateTime updated_at { get; set; }
}

public class RootObject
{
    public ResponseModel data { get; set; }
}

Upvotes: 2

Related Questions