Deserializing JSON into an object with Json.NET
Posted
by hmemcpy
on Stack Overflow
See other posts from Stack Overflow
or by hmemcpy
Published on 2010-03-31T18:15:02Z
Indexed on
2010/03/31
19:33 UTC
Read the original article
Hit count: 909
Hello.
I'm playing a little bit with the new StackOverflow API. Unfortunately, my JSON is a bit weak, so I need some help.
I'm trying to deserialize this JSON of a User:
{"user":{
"user_id": 1,
"user_type": "moderator",
"creation_date": 1217514151,
"display_name": "Jeff Atwood",
...
"accept_rate": 100
}}
into an object which I've decorated with JsonProperty
attributes:
[JsonObject(MemberSerialization.OptIn)]
public class User
{
[JsonProperty("user_id", Required = Required.Always)]
public virtual long UserId { get; set; }
[JsonProperty("display_name", Required = Required.Always)]
public virtual string Name { get; set; }
...
}
I get the following exception:
Newtonsoft.Json.JsonSerializationException: Required property 'user_id' not found in JSON.
Is this because the JSON object is an array? If so, how can I deserialize it to the one User object?
Thanks in advance!
© Stack Overflow or respective owner