-3

I am getting this response.How to deserilize the JSON?

[{
    "NoOfRecord":"2",
    "data":[ 
    {
    "name": "Pinky",
    "Id": "8989898",
    "PhoneNumber": "918934",
    "status": "Success",
    },
    {
    "name": "Kajol",
    "Id": "2345678",
    "PhoneNumber": "915566",
    "status": "Fail",
    }
    ]
}]

I tried like this, but getting error.

I have created 2 classes for that.

public class JsonResult2
{
    public string NoOfRecord{ get; set; }
    public JsonData Data { get; set; }
}

public class JsonData 
{
   public string name{ get; set; }
   public string Id{ get; set; }
   public string PhoneNumber{ get; set; }
   public string status{ get; set; }
}

When I am writing like this, getting error.

 var JsonData = JsonConvert.DeserializeObject<JsonResult2>(ResponseJson);

I am using Newtonsoft's library(using Newtonsoft.Json;)

How to access all name,id,phonenumber and status.

I am storing obtained response in string called ResponseJson.

Error : Cannot deserialize JSON array into type 'JsonResult2'

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Jui Test
  • 2,399
  • 14
  • 49
  • 76

1 Answers1

1

First of all, your JSON string is invalid. It has two commas as shown below which shouldn't be there:

[{
    "NoOfRecord":"2",
    "data":[ 
    {
    "name": "Pinky",
    "Id": "8989898",
    "PhoneNumber": "918934",
    "status": "Success", <-- This comma shouldn't be there
    },
    {
    "name": "Kajol",
    "Id": "2345678",
    "PhoneNumber": "915566",
    "status": "Fail", <-- This comma shouldn't be there
    }
    ]
}]

Once those commas are removed, your JSON is valid. Here's how it would look:

[{
    "NoOfRecord":"2",
    "data":[ 
    {
    "name": "Pinky",
    "Id": "8989898",
    "PhoneNumber": "918934",
    "status": "Success"
    },
    {
    "name": "Kajol",
    "Id": "2345678",
    "PhoneNumber": "915566",
    "status": "Fail"
    }
    ]
}]

Now the outermost brackets [] mean that you are receiving an array, not object. So your de-serialization syntax should be:

var JsonData = JsonConvert.DeserializeObject<List<JsonResult2>>(ResponseJson);

Then you need to ensure that the properties in the JSON match those of your entities. Also note the two [] square brackets against data in the JSON. That means you are expecting an array, not object. That means Data should be List<JsonData> instead of JsonData. So you'd need to update JsonResult2 as follows:

public class JsonResult2
{
    public string NoOfRecord { get; set; }
    public List<JsonData> Data { get; set; }
}

Also, if you are certain that NoOfRecord is a number, you might want to change the type of NoOfRecord within JsonResult2 to int.

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
  • @Downvoter: Care to explain what is wrong with my answer? – Nisarg Shah Aug 04 '17 at 12:36
  • 1
    I am not the downvoter. My guess would be because you however have provided the wrong answer. `Data` should be an collection of `JsonData` based on the JSON in the OP ie `public IList data { get; set; }` – Nkosi Aug 04 '17 at 12:39
  • @Nkosi thanks, I had missed that. – Nisarg Shah Aug 04 '17 at 12:42
  • You should also let the OP know that they are deserializing to the wrong type. everything is wrapped in an array so they need to ask for `JsonResult2[]` hence the error message about not being able to deserialize JSON array into type 'JsonResult2' – Nkosi Aug 04 '17 at 12:43
  • @Nkosi It's astonishing how many details I had missed. I hadn't even noticed that their JSON is invalid. There are a bunch of commas `,` before closing brackets `}`. I will update the answer. Thanks a lot! – Nisarg Shah Aug 04 '17 at 12:47