REST API的包装设计:返回动态JSON作为JSON.NET JObject / JArray [英] REST API wrapper design: returning dynamic json as JSON.NET JObject / JArray

查看:492
本文介绍了REST API的包装设计:返回动态JSON作为JSON.NET JObject / JArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个RESTful API JSON一个C#包装,并使用 Json.NET 反序列化传入的JSON强类型目的。但在传入JSON一些属性是高度动态的,这将是具有不同数目和属性的类型一些JSON对象。我目前的解决办法是,我映射的动态JSON属性词典<字符串对象> (嵌套字典)在我的C#类,并编写自定义JsonConverter动态JSON转换为嵌套字典。我的阶级是这样的:

I am writing a C# wrapper for a RESTful JSON API, and using Json.NET to deserialize the incoming json to strongly typed object. but a few properties in the incoming json are highly dynamic, it will be some json object with different number and type of properties. My current solution is, I mapped the dynamic json property to Dictionary<string, object> (Nested dictionary) in my C# class,and write Custom JsonConverter to convert dynamic json to Nested dictionary. My class look like this:

public class Item
{
  [JsonProperty("item_id")]
  public int ItemId { get; set; }

  [JsonProperty("type")]
  public string Type { get; set; }

  //Property to map dynamic json object
  [JsonProperty("data")]
  public Dictionary<string, object> Data { get; set; }
}

这是成功的。

问题是,它很难从这个嵌套的字典中访问数据的最终用户,我们甚至无法看到的就是数据的结构,而不在Visual Studio调试它。现在我打算使用<一个href=\"http://james.newtonking.com/json/help/index.html?topic=html/T_Newtonsoft_Json_Linq_JObject.htm\">JObject或<一个href=\"http://james.newtonking.com/json/help/index.html?topic=html/T_Newtonsoft_Json_Linq_JArray.htm\">JArray代替嵌套的字典。这样我们就可以通过只调用ToString方法(将输出原始的JSON数据字符串),以及该类型看数据的结构有LINQ支持,使用户可以从它轻松地访问数据。

The problem is, its very difficult to to access data from this nested dictionary for the end users, we can't even see what is the structure of data without debugging it in Visual studio. Now i am planning to use JObject or JArray instead of nested dictionary. So that we can see the structure of data by just calling ToString method (Will output the original json data as string) as well as this types have LINQ support so that users can access data from it easily.

这是一个很好的做法利用这一点,考虑<一个href=\"http://james.newtonking.com/json/help/index.html?topic=html/T_Newtonsoft_Json_Linq_JObject.htm\">JObject和<一个href=\"http://james.newtonking.com/json/help/index.html?topic=html/T_Newtonsoft_Json_Linq_JArray.htm\">JArray类型都依赖于第三方库(反正Json.NET是我的库的依赖性,因为我使用它的JSON序列化)。或者还有一些其他的方法可以做到这一点?

Is this a good practice use this, considering JObject and JArray types both depend on a third party library (Anyway Json.NET is a dependency for my library because i am using it for json serialization) . Or there some other ways to do this ?

推荐答案

您应该使用 ExpandoObjectConverter 代替。

您需要键入的IDictionary&LT;字符串对象&gt; 刚才动态和<装点全属性code> [JsonConverter(typeof运算(ExpandoObjectConverter))] 。

一个有趣的细节是 ExpandoObject 也农具的IDictionary&LT;字符串对象&gt; ,但是当你键入动态,您可以访问相关属性像普通的人! ;)

One interesting detail is ExpandoObject also implements IDictionary<string, object>, but when you type it with dynamic, you can access associated properties like regular ones! ;)

这篇关于REST API的包装设计:返回动态JSON作为JSON.NET JObject / JArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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