使用已知和未知字段反序列化 json [英] Deserialize json with known and unknown fields

查看:31
本文介绍了使用已知和未知字段反序列化 json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下 json 结果:默认的 json 结果有一组已知的字段:

<代码>{"id": "7908","name": "产品名称"}

但可以使用其他字段(在本例中为 _unknown_field_name_1_unknown_field_name_2)进行扩展,在请求结果时,这些字段的名称是未知的.

<代码>{"id": "7908","name": "产品名称","_unknown_field_name_1": "某个值",_unknown_field_name_2":一些值"}

我希望将 json 结果序列化和反序列化为具有已知字段属性的类,并将未知字段(没有属性)映射到一个属性(或多个属性),例如字典它们可以被访问和修改.

公共类产品{公共字符串 ID { 获取;放;}公共字符串名称 { 获取;放;}公共字典<字符串,字符串>字段{获取;放;}}

我想我需要一种方法来插入 json 序列化程序并自己为丢失的成员进行映射(用于序列化和反序列化).我一直在寻找各种可能性:

  • json.net 和自定义合同解析器(不知道怎么做)
  • datacontract 序列化程序(只能覆盖 onserialized、onserializing)
  • 序列化为动态并进行自定义映射(这可能有效,但似乎工作量很大)
  • 让产品继承自 DynamicObject(序列化程序使用反射,不调用 trygetmember 和 trysetmember 方法)

我使用的是restsharp,但可以插入任何序列化程序.

哦,我无法更改 json 结果,并且 this这个也没有帮助我.>

更新:这看起来更像:http://geekswithblogs.net/DavidHoerster/archive/2011/07/26/json.net-custom-convertersndasha-quick-tour.aspx

解决方案

解决这个问题的更简单的选择是使用 JsonExtensionDataAttribute 来自 JSON .NET

公共类MyClass{//已知字段公共十进制税率{得到;放;}//额外的字段[JsonExtensionData]private IDictionary_额外的东西;}

项目博客上有一个示例 这里

更新请注意,这需要 JSON .NET v5 版本 5 及更高版本

Given following json result: The default json result has a known set of fields:

{
    "id": "7908",
    "name": "product name"
}

But can be extended with additional fields (in this example _unknown_field_name_1 and _unknown_field_name_2) of which the names are not known when requesting the result.

{
    "id": "7908",
    "name": "product name",
    "_unknown_field_name_1": "some value",
    "_unknown_field_name_2": "some value"
}

I would like the json result to be serialized and deserialized to and from a class with properties for the known fields and map the unknown fields (for which there are no properties) to a property (or multiple properties) like a dictionary so they can be accessed and modified.

public class Product
{
    public string id { get; set; }
    public string name { get; set; }
    public Dictionary<string, string> fields { get; set; }
}

I think I need a way to plug into a json serializer and do the mapping for the missing members myself (both for serialize and deserialize). I have been looking at various possibilities:

  • json.net and custom contract resolvers (can't figure out how to do it)
  • datacontract serializer (can only override onserialized, onserializing)
  • serialize to dynamic and do custom mapping (this might work, but seems a lot of work)
  • let product inheriting from DynamicObject (serializers work with reflection and do not invoke the trygetmember and trysetmember methods)

I'm using restsharp, but any serializer can be plugged in.

Oh, and I cannot change the json result, and this or this didn't help me either.

Update: This looks more like it: http://geekswithblogs.net/DavidHoerster/archive/2011/07/26/json.net-custom-convertersndasha-quick-tour.aspx

解决方案

An even easier option to tackling this problem would be to use the JsonExtensionDataAttribute from JSON .NET

public class MyClass
{
   // known field
   public decimal TaxRate { get; set; }

   // extra fields
   [JsonExtensionData]
   private IDictionary<string, JToken> _extraStuff;
}

There's a sample of this on the project blog here

UPDATE Please note this requires JSON .NET v5 release 5 and above

这篇关于使用已知和未知字段反序列化 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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