反序列化期间JSON.Net忽略物业 [英] JSON.Net Ignore Property during deserialization

查看:133
本文介绍了反序列化期间JSON.Net忽略物业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类设置如下:

public class Foo
{
    public string string1 { get; set; }
    public string string2 { get; set; }
    public string string3 { get; set; }
}



我使用Json.Net反序列化以下JSON响应:

I am using Json.Net to deserialize the following Json Response:

string json = "[{\"number1\": 1, \"number2\": 12345678901234567890, \"number3\": 3},      
{\"number1\": 9, \"number2\": 12345678901234567890, \"number3\": 8}]";



反序列化代码:

Deserialization code:

List<Foo> foos = JsonConvert.DeserializeObject<List<Foo>>(json);



第二个数字超过了一个int-64,但我真的不关心检索该值。有没有一种方法来'编号2'属性转换为字符串,或完全反序列化过程中忽略它?

The second number exceeds an int-64, but I don't really care about retrieving that value. Is there a way to cast the 'number2' property to a string, or fully ignore it during deserialization?

我曾尝试加入[JsonConverter(typeof运算(字符串) )属性添加到字符串2财产,但收到的错误:错误创建System.String。我也曾尝试设置的typeof(十进制)。

I have tried adding the '[JsonConverter(typeof(string))]' attribute to the string2 property, but recieve the error: 'Error creating System.String'. I have also tried setting typeOf(decimal).

我也尝试使用[JsonIgnore],但不起作用。

I have also tried using [JsonIgnore] but that doesn't work.

在此先感谢!

推荐答案

您可以使用 MissingMemberHandling JsonSerializerSettings 对象

使用范例:

jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;

JsonConvert.DeserializeObject<YourClass>(jsonResponse, jsonSerializerSettings);



更多信息的这里

这篇关于反序列化期间JSON.Net忽略物业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆