用Json.NET将JSON反序列化为一个对象。

时间:2022-08-23 07:57:26

I'm playing a little bit with the new * API. Unfortunately, my JSON is a bit weak, so I need some help.

我使用了一些新的* API。不幸的是,我的JSON有点弱,所以我需要一些帮助。

I'm trying to deserialize this JSON of a User:

我试图反序列化一个用户的JSON:

  {"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:

我用JsonProperty属性对其进行了修饰:

[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.

Newtonsoft.Json。JsonSerializationException: Required属性'user_id'没有在JSON中找到。

Is this because the JSON object is an array? If so, how can I deserialize it to the one User object?

这是因为JSON对象是一个数组吗?如果是,我如何将它反序列化为一个用户对象?

Thanks in advance!

提前谢谢!

3 个解决方案

#1


26  

As Alexandre Jasmin said in the comments of your question, the resulting JSON has a wrapper around the actual User object you're trying to deserialize.

正如Alexandre Jasmin在您的问题的评论中所说,生成的JSON在您试图反序列化的实际用户对象周围有一个包装器。

A work-around would be having said wrapper class:

一个变通方法是说包装类:

public class UserResults
{
    public User user { get; set; }
}

Then the deserialization will work:

那么反序列化将会奏效:

using (var sr = new StringReader(json))
using (var jr = new JsonTextReader(sr))
{
    var js = new JsonSerializer();
    var u = js.Deserialize<UserResults>(jr);
    Console.WriteLine(u.user.display_name);
}

There will be future metadata properties on this wrapper, e.g. response timestamp, so it's not a bad idea to use it!

在这个包装器上将有未来的元数据属性,例如响应时间戳,所以使用它不是一个坏主意!

#2


37  

If you don't want to create a wrapper class you can also access the User this way:

如果不想创建包装类,也可以通过以下方式访问用户:

String jsonString = "{\"user\":{\"user_id\": 1, \"user_type\": \"moderat...";
JToken root = JObject.Parse(jsonString);
JToken user = root["user"];
User deserializedUser = JsonConvert.DeserializeObject<User>(user.ToString());

See this page in the Json.NET doc for details.

请参见Json中的这个页面。净文档细节。

#3


5  

Similar to @Alexandre Jasmin's answer, you can use an intermediary JsonSerializer to convert instead of the using the high-level JsonConvert on .ToString(). No idea if it's any more efficient...

类似于@Alexandre Jasmin的答案,您可以使用中间的JsonSerializer来进行转换,而不是在. tostring()上使用高级JsonConvert。不知道这样是否更有效……

References:

引用:

Example:

例子:

var root = JObject.Parse(jsonString);
var serializer = new JsonSerializer();
var expectedUserObject = serializer.Deserialize<User>(root["user"].CreateReader());

#1


26  

As Alexandre Jasmin said in the comments of your question, the resulting JSON has a wrapper around the actual User object you're trying to deserialize.

正如Alexandre Jasmin在您的问题的评论中所说,生成的JSON在您试图反序列化的实际用户对象周围有一个包装器。

A work-around would be having said wrapper class:

一个变通方法是说包装类:

public class UserResults
{
    public User user { get; set; }
}

Then the deserialization will work:

那么反序列化将会奏效:

using (var sr = new StringReader(json))
using (var jr = new JsonTextReader(sr))
{
    var js = new JsonSerializer();
    var u = js.Deserialize<UserResults>(jr);
    Console.WriteLine(u.user.display_name);
}

There will be future metadata properties on this wrapper, e.g. response timestamp, so it's not a bad idea to use it!

在这个包装器上将有未来的元数据属性,例如响应时间戳,所以使用它不是一个坏主意!

#2


37  

If you don't want to create a wrapper class you can also access the User this way:

如果不想创建包装类,也可以通过以下方式访问用户:

String jsonString = "{\"user\":{\"user_id\": 1, \"user_type\": \"moderat...";
JToken root = JObject.Parse(jsonString);
JToken user = root["user"];
User deserializedUser = JsonConvert.DeserializeObject<User>(user.ToString());

See this page in the Json.NET doc for details.

请参见Json中的这个页面。净文档细节。

#3


5  

Similar to @Alexandre Jasmin's answer, you can use an intermediary JsonSerializer to convert instead of the using the high-level JsonConvert on .ToString(). No idea if it's any more efficient...

类似于@Alexandre Jasmin的答案,您可以使用中间的JsonSerializer来进行转换,而不是在. tostring()上使用高级JsonConvert。不知道这样是否更有效……

References:

引用:

Example:

例子:

var root = JObject.Parse(jsonString);
var serializer = new JsonSerializer();
var expectedUserObject = serializer.Deserialize<User>(root["user"].CreateReader());