是否有过分配多个Jsonproperties的方法吗? [英] Is there a way too assign multiple Jsonproperties?
问题描述
我想使保存来自Facebook和Twitter信息的单个数据类。
但在从Twitter我的JSON的答复,我需要ID_STR,并从Facebook我得到的身份证。
我需要这两个被放入了ID-string。
现在我知道我可以使用[JsonProperty(ID_STR)]如果我想Twitter的反序列化到ID_STR我的ID-string。但如果我同时需要Facebook的ID和Twitter的ID_STR在同一个ID反序列化-string我有我的数据类?
没有,这是不可能的。
让我们来看看在的 Json.NET文档。在讲究 JsonPropertyAttribute 类的帮助页面。
要报价:
指示JsonSerializer总是序列化的成员具有指定名称
它在Newtonsoft.Json命名空间中声明。我们需要确定它是如何声明。让我们来看看在CodePlex上的Json.NET的源代码:
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property |
AttributeTargets.Parameter,的AllowMultiple = FALSE)]
公共密封类JsonPropertyAttribute:属性
{
// ...
}
想这回答了这个问题。该属性的的AllowMultiple 属性设置为false。所以,你可以不装饰的属性,字段或参数比此属性一次。
即使你能你怎么能指望Json.net找出哪些属性使用?
<$:我想创建类型为Twitter和Facebook分别在其中你可以反序列化接收到的JSON数据
所以, p $ p> 微博 - > JSON - > Twitter的特定类型的
Facebook的 - > JSON - > Facebook的spefic类型
然后创建一个应用程序使用,而不是直接处理这些类型的抽象。他们只是属于一个特定的社交媒体实施
的Twitter / Facebook的/ ... speficic类型 - >您的类型
如果您直接尝试将数据反序列化到常用类型,那么你只是要保持挣扎,因为它们不与接收到的数据对齐100%,你会用一些时髦风,难维持反序列化逻辑。
另一个选择是创建自己的定制Json.NET转换器。
的 http://geekswithblogs.net/DavidHoerster/存档/ 2011/7月26日/ json.net定制-convertersndasha-快速tour.aspx
刚刚创建Twitter和Facebook,当转换器你反序列化JSON数据,就指定要使用的转换器。
- TwitterConverter
- FacebookConverter
例如:
MySocialType的myType = JsonConvert.DeserializeObject&所述;映射>(JSON,
新TwitterConverter());
无论如何,我会尽力避免与反序列化逻辑本身污染类的类型。
I am trying to make a single dataclass that holds information from both Facebook and Twitter.
but in my JSON reply from twitter I need "id_str" and from facebook I get "id".
I need those two to be put into the "id"-string.
Now I know I can use [JsonProperty("id_str")] if I want to deserialize Twitters id_str into my "id"-string. But what if I need both Facebook's "id" and Twitters "id_str" to be deserialized in the same "id"-string I have in my dataclass?
No, this is not possible.
Let's take a look at the Json.NET documentation. In particular the help page about the JsonPropertyAttribute class.
To quote:
"Instructs the JsonSerializer to always serialize the member with the specified name."
It's declared in the Newtonsoft.Json namespace. We need to determine how it is declared. Let's take a look at the Json.NET's source code on CodePlex:
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property |
AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class JsonPropertyAttribute : Attribute
{
//...
}
Guess that answers the question. The AllowMultiple property of the attribute is set to false. So you can't decorate a property, field or parameter more than once with this attribute.
Even if you could how do you expect Json.net to figure out which attribute to use? I would create types for Twitter and Facebook separately into which you can deserialize the received JSON data.
So:
Twitter -> JSON -> Twitter specific types
Facebook -> JSON -> Facebook spefic types
Then create an abstraction which your application uses instead of addressing these types directly. They just belong to a specific social media implementation.
Twitter / Facebook / ... speficic types -> Your types
If you directly try to deserialize the data into your "common types", then you are just going to keep struggling because they don't align 100% with the received data and you'll wind up with some funky, hard to maintain deserialization logic.
Another option is to create your own custom Json.NET converter.
Just create a converter for Twitter and Facebook and when you deserialize the JSON data, just specify which converter you want to use.
- TwitterConverter
- FacebookConverter
E.g.:
MySocialType myType = JsonConvert.DeserializeObject<Mapped>(json,
new TwitterConverter());
Anyway I would try to avoid polluting your class types with the deserialization logic itself.
这篇关于是否有过分配多个Jsonproperties的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!