bruchowski
bruchowski

Reputation: 5351

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. 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? 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)

Here is an example of the final json I want when I serialize it.

...
"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

My final solution was to use the JArray object. A JArray is a JContainer, which is a JToken, which you can add to a JObject. My problem was that I was trying to use a JObject[], which was not a valid JToken

Upvotes: 13

Views: 25647

Answers (2)

Rob G
Rob G

Reputation: 3526

Check out the PopulateObject() method, it has a good example of how to do this:

http://james.newtonking.com/projects/json/help/index.html?topic=html/PopulateObject.htm

Upvotes: 0

bruchowski
bruchowski

Reputation: 5351

My final solution was to use the JArray object. A JArray is a JContainer, which is a JToken, which you can add to a JObject. My problem was that I was trying to use a JObject[], which was not a valid JToken

Upvotes: 19

Related Questions