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 Pro5.
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"]
但是以上几行都失败了.
But the above lines fail.
我在上面使用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 = o["Object1"];
这篇关于JContainer,JObject,JToken和Linq混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!