Naveen
Naveen

Reputation: 315

Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[obJson]'

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; }
}

Upvotes: 0

Views: 14242

Answers (3)

Muhammad Adeel Zahid
Muhammad Adeel Zahid

Reputation: 17784

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.

Upvotes: 1

L.B
L.B

Reputation: 116108

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);

Upvotes: 2

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26727

your json is not valid.

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

http://jsonlint.com/

Upvotes: 0

Related Questions