[英]How to add a json array into a property of a JObject with json.net
I am having difficulty figuring out how to add an array of json objects to an existing JObject
. 我很难搞清楚如何将json对象数组添加到现有的JObject
。 Say I have a JObject
with just the "Modified" property, and I want to add another property "IntersectGroups" that contains an array of json objects, how can I do this? 假设我有一个只有“Modified”属性的JObject
,我想添加另一个属性“IntersectGroups”,其中包含一个json对象数组,我该怎么做? I Have a JObject[]
that when I serialize it is exactly in the format I require, but I am looking for something like this: mainJObject.Add("IntersectGroups", myJObjectArray)
我有一个JObject[]
,当我序列化时,它正是我需要的格式,但我正在寻找这样的东西: mainJObject.Add("IntersectGroups", myJObjectArray)
Here is an example of the final json I want when I serialize it. 这是我序列化时想要的最终json的一个例子。
...
"Modified": "2012-11-26T10:21:04.693",
"IntersectGroups": [
{
"Id": 1004,
"UserId": 20003,
"GroupId": 1001,
"Admin": false,
"Expires": "1900-01-01T00:00:00"
},
{
"Id": 1003,
"UserId": 20003,
"GroupId": 1000,
"Admin": false,
"Expires": "1900-01-01T00:00:00"
}
]
...
UPDATE UPDATE
My final solution was to use the JArray object. 我的最终解决方案是使用JArray对象。 A JArray is a JContainer, which is a JToken, which you can add to a JObject. JArray是一个JContainer,它是一个JToken,可以添加到JObject中。 My problem was that I was trying to use a JObject[], which was not a valid JToken 我的问题是我试图使用JObject [],这不是一个有效的JToken
My final solution was to use the JArray
object. 我的最终解决方案是使用JArray
对象。 A JArray
is a JContainer
, which is a JToken
, which you can add to a JObject
. JArray
是一个JContainer
,它是一个JToken
,可以添加到JObject
。 My problem was that I was trying to use a JObject[]
, which was not a valid JToken
我的问题是我试图使用JObject[]
,这不是一个有效的JToken
Check out the PopulateObject()
method, it has a good example of how to do this: 查看PopulateObject()
方法,它有一个很好的示例,说明如何执行此操作:
http://james.newtonking.com/projects/json/help/index.html?topic=html/PopulateObject.htm http://james.newtonking.com/projects/json/help/index.html?topic=html/PopulateObject.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.