JContainer、JObject、JToken 和 Linq 混淆 [英] JContainer, JObject, JToken and Linq confusion
问题描述
我无法理解何时使用 JContainer
、JObject
和 JToken
.我从标准"中了解到 JObject
由 JProperties
组成,并且 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
变成一个 JObject
code>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
因为我不知道我应该使用什么对象类型:JContainer
、JObject
或 JToken代码>!为了让您知道我想要做什么,一旦正确分配了上述
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?
当 JToken
和 JObject
对象似乎与 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 元素的基类.JObject
、JArray
、JProperty
和 JConstructor
都继承自它.
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 混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!