JSON.NET:为什么要使用JToken? [英] JSON.NET: Why Use JToken--ever?

查看:88
本文介绍了JSON.NET:为什么要使用JToken?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为对我这里问题的跟进: JSON.NET:从JProperty值获取JObject ...

我阅读了链接并消化了以上评论和建议.现在我想知道:是否有充分的理由说明为什么没有直接"(即简单)的方法将JProperty对象的值转换为JObject?到达JToken,然后必须构造if语句,这似乎是很多工作.相反,它是承认我仍然认为我不理解JToken的真正目的.一次,所有FedEx程序包都首先路由到孟菲斯:似乎所有对象都可以/应该首先转到JToken,然后再打包为实际的目标对象类型.这是一种思考的方式吗?

换句话说,是否有充分的理由使用JToken?或者仅仅是其他许多函数返回了JToken,然后您就必须处理它? JSON.NET手册提供了将JToken转换为其他类型的方法( http://www .newtonsoft.com/json/help/html/Operators_T_Newtonsoft_Json_Linq_JToken.htm ),但没有提到从JToken到JObject ...

我发现JObject通常是我想要的,以便使用JSON并将其从JSON映射到我的.NET类,然后再返回-以及执行许多其他操作.我仍然想知道曾经使用JToken对象的令人信服的原因是什么?

解决方案

标准开始,JSON是由以下五种类型的令牌构建而成:

  • object :一组无序的名称/值对.
  • array :值的有序集合.
  • :用双引号引起来的字符串,或者是数字,或者是truefalsenull,或者对象或数组.这些结构可以嵌套.
  • 字符串
  • 编号.

JToken 是一个抽象基类,它代表任何这些可能的标记之一.如果您有一些JSON并且事先不知道其中可能包含什么,则可以使用 JObject.Parse() JValue.Parse() 来解析您知道的JSON字符串表示原子"值,在这种情况下需要使用JToken.Parse().

类似地,可以使用 JToken.FromObject() 进行序列化某种JToken层次结构的c#对象,而无需事先知道结果JSON类型.这可能是有用的,例如编写与序列化相关的通用代码时.

As a follow up to my question here: JSON.NET: Obtain JObject from JProperty Value ...

I read links and digested the above comments and advice. Now I wonder: Is there a good reason why there is no "direct" (i.e., easy) way to turn the Value of a JProperty object into a JObject? It seems like a lot of work to get to a JToken and then have to construct if statements, etc. This isn't to complain about the extra work; rather, it's to admit that I still think I don't understand the true purpose of JToken. At one time, all FedEx packages first routed to Memphis: It seems that all objects can/should go to JToken first then be parceled to the actual target object type. Is that a way to think about it?

In other words, is there ever a good reason to use JToken--or is it just that so many other functions return a JToken and then you have to just deal with that? The JSON.NET manual gives ways to cast JToken to other types (http://www.newtonsoft.com/json/help/html/Operators_T_Newtonsoft_Json_Linq_JToken.htm) but doesn't mention going from JToken to JObject...

I find that JObject is usually what I want to have in order to work with JSON and to map from JSON to my .NET classes and back again--as well as doing a host of other operations. I still wonder what is the compelling reason to ever use a JToken object?

解决方案

From the standard, JSON is built out of the following five types of token:

  • object: an unordered set of name/value pairs.
  • array: an ordered collection of values.
  • value: a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
  • string
  • number.

JToken is an abstract base class that represents any one of these possible tokens. If you have some JSON and don't know in advance what might be inside, you can parse it with JToken.Parse() and get a result as long as the JSON is well-formed. JObject.Parse() and JArray.Parse() will throw if the root JSON token is not of the expected type. And there is no JValue.Parse() to parse a JSON string you know to represent an "atomic" value, requiring the use of JToken.Parse() in such a case.

Similarly, JToken.FromObject() may be used to serialize any sort of c# object to a JToken hierarchy without needing to know in advance the resulting JSON type. This can be useful e.g. when writing generic serialization-related code.

这篇关于JSON.NET:为什么要使用JToken?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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