分配多个JsonProperty? [英] Assign Multiple JsonProperties?

查看:272
本文介绍了分配多个JsonProperty?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个包含来自Facebook和Twitter的信息的数据类.

I am trying to make a single dataclass that holds information from both Facebook and Twitter.

但是在Twitter的JSON答复中,我需要id_str,从FaceBook中得到id.

but in my JSON reply from twitter I need id_str and from FaceBook I get id.

我需要将这两个放在id字符串中.

I need those two to be put into the id-string.

现在,如果我想将Twitters id_str反序列化为我的id -string,我知道可以使用[JsonProperty("id_str")].

Now I know I can use [JsonProperty("id_str")] if I want to deserialize Twitters id_str into my id-string.

但是,如果我需要将Facebook的id和Twitter的id_str都以与数据类中相同的id字符串反序列化,该怎么办?

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?

推荐答案

否,这是不可能的.

让我们看一下 Json.NET文档.特别是有关 JsonPropertyAttribute 类的帮助页面.

Let's take a look at the Json.NET documentation. In particular the help page about the JsonPropertyAttribute class.

引用:

指示JsonSerializer始终序列化具有指定名称的成员."

"Instructs the JsonSerializer to always serialize the member with the specified name."

它在Newtonsoft.Json命名空间中声明.我们需要确定如何声明它.让我们看一下CodePlex上Json.NET的源代码:

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:

http://json.codeplex.com/

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property |     
 AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class JsonPropertyAttribute : Attribute
{
    //...
}

猜猜回答了这个问题.该属性的 AllowMultiple 属性设置为false.因此,使用该属性装饰属性,字段或参数不能一次以上.

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.

即使您可以如何期望Json.net找出要使用的属性?我将分别为Twitter和Facebook创建类型,您可以在这些类型中反序列化接收到的JSON数据.

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.

所以:

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/...特殊类型->您的类型

Twitter / Facebook / ... speficic types -> Your types

如果您直接尝试将数据反序列化为常见类型",那么您将一直在努力,因为它们不会与接收到的数据100%对齐,并且您会发现一些时髦,难以理解的数据保持反序列化逻辑.

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.

另一种选择是创建自己的自定义Json.NET转换器.

Another option is to create your own custom Json.NET converter.

http://geekswithblogs.net/DavidHoerster/archive/2011/07/26/json.net-custom-convertersndasha-quick-tour.aspx

只需为Twitter和Facebook创建一个转换器,然后反序列化JSON数据时,只需指定要使用的转换器即可.

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

例如:

MySocialType myType = JsonConvert.DeserializeObject<Mapped>(json, 
    new TwitterConverter());

无论如何,我会尽量避免使用反序列化逻辑本身来污染您的类类型.

Anyway I would try to avoid polluting your class types with the deserialization logic itself.

这篇关于分配多个JsonProperty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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