JContainer、JObject、JToken和Linq混淆

时间:2023-04-26
本文介绍了JContainer、JObject、JToken和Linq混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我无法理解何时使用 JContainerJObjectJToken.我从标准"中了解到 JObjectJProperties 组成,并且 JToken 是所有 JToken 的基本抽象类 类型,但我不明白 JContainer.

I am having trouble understanding when to use JContainer, JObject, and JToken. I understand from the "standards" that JObject is composed of JProperties and that JToken is the base abstract class for all of the JToken types, but I don't understand JContainer.

我正在使用 C#,我刚刚购买了 LinqPad Pro 5.

I am using C# and I just bought LinqPad Pro 5.

我在一个文件中有一个 JSON 数据源,因此我正在使用以下语句成功地反序列化该文件的内容:

I have a JSON data source in a file, so I'm deserializing that file's contents successfully using this statement:

string json;
using (StreamReader reader = new StreamReader(@"myjsonfile.json"))
{
    json = reader.ReadToEnd();
}

此时,我将 JSON 字符串对象反序列化为 JObject (这可能是我的错误——也许我需要将 jsonWork 设为 JToken 还是 JContainer?):

At that point, I take the JSON string object and deserialize it to a JObject (and this might be my mistake--perhaps I need to make jsonWork a JToken or JContainer?):

JObject jsonWork = (JObject)JsonConvert.DeserializeObject(json);

在我的 JSON 数据(JSON 表示的字符串)中,我有三个对象——顶级对象看起来类似于:

In my JSON data (the string represented by JSON), I have three objects--the top-level object look similar to this:

{
  "Object1" : { ... },
  "Object2" : { ... },
  "Object3" : { ... }
}

每个对象都由各种标记(数组、字符串、其他对象等)组成,因此它是动态 JSON.(我使用省略号作为占位符,而不是用大量 JSON 数据混淆这个问题.)

Each object is composed of all sorts of tokens (arrays, strings, other objects, etc.), so it is dynamic JSON. (I used ellipses as placeholders rather than muddying up this question wit lots of JSON data.)

但是,我想使用 LINQ 分别处理 "Object1""Object2""Object3".所以,理想情况下,我想要这样的东西:

I want to process "Object1", "Object2", and "Object3" separately using LINQ, however. So, ideally, I would like something like this:

// these lines DO NOT work    
var jsonObject1 = jsonWork.Children()["Object1"]
var jsonObject2 = jsonWork.Children()["Object2"]
var jsonObject3 = jsonWork.Children()["Object3"]

但以上几行都失败了.

我在上面使用了 var,因为我不知道应该使用什么对象类型:JContainerJObjectJToken!只是为了让您知道我想要做什么,一旦正确分配了上述 jsonObject# 变量,我想使用 LINQ 来查询它们包含的 JSON.这是一个非常简单的例子:

I used var above because I have no idea what object type I should be using: JContainer, JObject, or JToken! Just so you know what I want to do, once the above jsonObject# variables are properly assigned, I would like to use LINQ to query the JSON they contain. Here is a very simple example:

var query = from p in jsonObject1
   where p.Name == "Name1"
   select p

当然,我的 LINQ 最终会在 jsonObject 变量中过滤 JSON 数组、对象、字符串等.我想一旦开始,我可以使用 LinqPad 帮助我使用 LINQ 过滤 JSON.

Of course, my LINQ ultimately will filter for JSON arrays, objects, strings, etc., in the jsonObject variable. I think once I get going, I can use LinqPad to help me filter the JSON using LINQ.

我发现如果我使用:

// this line WORKS 
var jsonObject1 = ((JObject)jsonWork).["Object1"];

然后我在 jsonObject1 中得到一个 JObject 类型.这是正确的方法吗?

Then I get an JObject type in jsonObject1. Is this the correct approach?

JTokenJObject 对象似乎可以很好地与 LINQ 配合使用时,我不清楚何时/为什么会使用 JContainer.JContainer的目的是什么?

It is unclear to me when/why one would use JContainer when it seems that JToken and JObject objects work with LINQ quite well. What is the purpose of JContainer?

推荐答案

JContainer 是具有子项的 JSON 元素的基类.JObjectJArrayJPropertyJConstructor都继承自它.

JContainer is a base class for JSON elements that have child items. JObject, JArray, JProperty and JConstructor all inherit from it.

例如下面的代码:

(JObject)JsonConvert.DeserializeObject("[1, 2, 3]")

会抛出 InvalidCastException,但如果你将它转换为 JContainer,那就没问题了.

Would throw an InvalidCastException, but if you cast it to a JContainer, it would be fine.

关于你原来的问题,如果你知道你在顶层有一个 JSON 对象,你可以使用:

Regarding your original question, if you know you have a JSON object at the top level, you can just use:

var jsonWork = JObject.Parse(json);
var jsonObject1 = jsonWork["Object1"];

这篇关于JContainer、JObject、JToken和Linq混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!