如何忽略JSON.NET中返回之间更改的数据 [英] How to ignore data in JSON.NET that changes between returns

查看:86
本文介绍了如何忽略JSON.NET中返回之间更改的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析第三方返回的JSON数据。



我使用JSON2CSharp生成了我的类,该类适用于我们收到的第一个示例。我对其进行了调整,使其具有一些JsonProperty设置,以便它不需要某些并不总是存在的属性。



现在,我收到了更多示例,并且其中一个datablob更改了格式



无需提供

  public Translations翻译{get;组; } 

 公共列表<翻译>翻译{get;组; } 

但是,blob是我们不需要的信息,无论是性能还是不必处理以及我们不需要更改格式的其他信息,反序列化时最好忽略它。



现在真正的问题是, JsonIgnore是否应该是否忽略整个数据块,无论数据的格式是否与该类中定义的格式不同?还是我必须为此编程?



所以如果我这样做

  [JsonIgnore] 
公共字符串翻译{组; }

在发送列表或对象时还会忽略翻译吗?



我可以对JsonIgnore使用与JsonProperty相同的语法,只是说

  [JsonIgnore(PropertyName = translations)] 

还是JsonIgnore只是扔掉收到的东西? / p>

另外一个问题:



是否没有任何翻译约定,我得到:

 翻译:[] 

,当有翻译时,我得到:

  translations:{ CA: blabla, DD: C:blablah} 

这是否可能是第三方网站中的错误?



添加:



1:每次获取JSON时,翻译都可以在字符串,列表和对象之间切换



2:由于使用DataMembers忽略了我实际上不需要的所有东西,因此一个带有子类的类,我是否必须告诉它子类是[DataMember]还是子类属性是[DataMember]?

解决方案

我将使用 DataContractAttribute DataMemberAttribute s在我的数据类中明确指定要序列化/反序列化的属性。



这是选择加入的,因此不会尝试将JSON中的任何多余内容以及数据中的多余内容数据类不会显示在序列化的JSON中。



因此,假设您的类现在看起来像这样:

  class MyData {
//老成员
// public Translations翻译{get;组; }

公共列表<翻译>翻译{get;组; }

公共字符串ThisShouldBeSerialized {get;组; }
}

您可以更改它,以便将要序列化的内容明确标记为,默认情况下会忽略未标记为要序列化的任何内容:

  [DataContract] 
class MyData {
//老会员
// // Public Translations Translations {get;组; }

公共列表<翻译>翻译{get;组; }

[DataMember]
公共字符串ThisShouldBeSerialized {get;组; }
}

然后:

  var myJSON = @ {
'ThisShouldBeSerialized':'test',
'Translations':{
'Some':'Strange' ,
'Data':'Blob'
}
};

var结果= JsonConvert.DeserializeObject< MyData>(myJSON);


I'm parsing JSON data return by a third party.

I have my class generated with JSON2CSharp which works for the first sample we received. I tweaked it to have some JsonProperty settings so that it doesn't require certain properties that are not always present.

Now I received more samples and one of the datablobs changed format

from needing

public Translations Translations { get; set; }

to

public List<Translations> Translations { get; set; }

The blob however is information we do not need, for both performance and not having to deal with that and other pieces of information we do not need changing format, it would be ideal to just ignore it when deserializing it.

Now the real question is, should "JsonIgnore" just ignore the entire blob of data irregardless if it is in a different format then defined in the class? Or do I have to program around that?

So if I do

[JsonIgnore]
public string Translations { get; set; }

will it also ignore Translations when it gets sent a list or an object?

Can I use the same syntax with JsonIgnore as I can with JsonProperty and just say

[JsonIgnore(PropertyName = "translations")]

or does JsonIgnore just toss out anything it receives?

Additionally question:

Is it convention that when there are no translations, I get:

"translations":[]

and when there are translations I get:

"translations":{"CA":"blabla","DD":"C : blablah"}

Or is this likely a bug in the third party's website?

ADDED:

1: The Translations can switch between string, list and object between every fetch of the JSON.

2: For using DataMembers ignoring everything I don't actually need, in a class with subclasses, do I have to tell it that the subclass is [DataMember] or the subclasses properties are [DataMember]?

解决方案

I would explicitly specify exactly the properties I wanted serialized/deserialized in my data class using DataContractAttribute and DataMemberAttributes for the members you actually want to deserialize.

This is opt in, so no attempt is made to shoehorn anything extra in your JSON into your data class, and anything extra in your data class doesn't show up in serialized JSON.

So assuming your class right now looks like this:

class MyData {
    // Old member
    // public Translations Translations { get; set; }

    public List<Translation> Translations { get; set; }

    public string ThisShouldBeSerialized { get; set; }
}

You can change it so things that you want serialized are explicitly marked as such, and anything not marked for serialization is ignored by default:

[DataContract]
class MyData {
    // Old member
    // public Translations Translations { get; set; }

    public List<Translation> Translations { get; set; }

    [DataMember]
    public string ThisShouldBeSerialized { get; set; }
}

And then:

var myJSON = @"{ 
    'ThisShouldBeSerialized': 'test', 
    'Translations': { 
         'Some' : 'Strange', 
         'Data' : 'Blob' 
     }
}";

var result = JsonConvert.DeserializeObject<MyData>(myJSON);

这篇关于如何忽略JSON.NET中返回之间更改的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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