0

I am trying to Deserialize (using Newtonsoft) JSON and convert to List in c#. It is throwing me error " Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[obJson]'."

Here is my JSON string:

  string webContent = "{\"searchResults\":     [{\"gefId\":0,\"resultNumber\":1,\"distance\":4.2839,\"sourceName\":\"MQA.MQ_34172_HD\",\"name\":\"USER_DEFINED\"},{\"gefId\":0,\"resultNumber\":1,\"distance\":4.2839,\"sourceName\":\"MQA.MQ_34172_HD\",\"name\":\"USER_DEFINED\"}]}";

Conversion, this line is throwing error:

  List<obJson> result = JsonConvert.DeserializeObject<List<obJson>>(webContent);

My custom classes:

public class SearchResults
{
    public int gefId { get; set; }
    public int resultNumber { get; set; }
    public decimal distance { get; set; }
    public string sourceName { get; set; }
    public string name { get; set; }
}

public class obJson
{
    public SearchResults SearchResults { get; set; }
}

3 Answers 3

2

Since your json is an object whose searchResults member contains an array, change your obJson as below

public class obJson
{
    public List<SearchResults> searchResults { get; set; }
}

and deserialize as

obJson result = JsonConvert.DeserializeObject<obJson>(webContent);
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this. Now there is no error but I am getting searchResult as null after deserialization.
@Naveen have you changed SearchResults to searchResults ? It works on my computer.
Yes I did. Here is the code. I am still getting sResult as null. public class obJson { public List<SearchResults> sResult { get; set; } } webContent = "{\"searchResults\":[{\"gefId\":0,\"resultNumber\":1,\"distance\":4.2839,\"sourceName\":\"MQA.MQ_34172_HD\",\"name\":\"USER_DEFINED\"},{\"gefId\":0,\"resultNumber\":1,\"distance\":4.2839,\"sourceName\":\"MQA.MQ_34172_HD\",\"name\":\"USER_DEFINED\"}]}"; obJson result = JsonConvert.DeserializeObject<obJson>(webContent);
But your list's name is sResult. Change it to searchResults. Or Just copy from my answer don't rename it (You can use also JsonProperty attribute after you get your code working).
sorry. Got it. Changed it to searchResults. It's working now. Thank you!
1

The problem is with your model or conversely with data you are sending. You are receiving an array and hoping to deserialize it into plain object. You can change your model like

public class obJson
{
    public SearchResults[] SearchResults { get; set; }
}

and your result will be deserialized just fine.

Comments

0

your json is not valid.

Parse error on line 1:
{    \"searchResults\": [
-----^
Expecting 'STRING', '}'

http://jsonlint.com/

3 Comments

Those are the escape characters which I am getting after issuing web request. If I remove array [] then I am able to retrieve single object after Deserializing. I am having problem JSON array [].
in your c# object use a List instead of an array
Try this {"searchResults":[{"gefId":0,"resultNumber":1,"distance":4.2839,"sourceName":"MQA.MQ_34172_HD","name":"USER_DEFINED"},{"gefId":0,"resultNumber":1,"distance":4.2839,"sourceName":"MQA.MQ_34172_HD","name":"USER_DEFINED"}]}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.