briba
briba

Reputation: 2987

Deserialize with Json.NET

I was searching for tutorials on how to deserialize objects using the Json.NET but i can't find any similar with mine.

I have a Json like this:

{
    "__ar":1,
    "payload":{
        "entries":[
        {
        "uid":100000011980844,
        "photo":"",
        "type":"user",
        "text":"testing",
        "path":"testing",
        "category":"",
        "needs_update":true,
        "non_title_tokens":"",
        "names":[
        "Test"
        ],
        "subtext":"",
        "score":0
        }
    ]
    }
}

And then, I tried to deserialize like this:

var j = JsonConvert.DeserializeObject<People> (json);

And then:

public class People
{
    public string __ar { get; set; }
    public Payload payload { get; set; }
}

public class Payload
{
    public Person entries { get; set; }
}

public class Person
{
    public string uid { get; set; }
    public string photo { get; set; }
    public string type { get; set; }
    public string text { get; set; }
    public string path { get; set; }
    public string category { get; set; }
    public string needs_update { get; set; }
    public string non_title_tokens { get; set; }
    public string subtext { get; set; }
    public string score { get; set; }
    public List<string> names { get; set; }
}

But it didn't work, anyone knows what do i have to do to fix it? The error message is:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'QueryFacebook.Parser+Person' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array.

Upvotes: 0

Views: 371

Answers (1)

I4V
I4V

Reputation: 35363

Just change the Payload definition as below and it will work

public class Payload
{
    public Person[] entries { get; set; }
}

Upvotes: 3

Related Questions