I've got a json array that I want to add to, then write the content to a file. I have no problem adding the content to the array, but writing to file using the JsonSerializer.Serialize method gives me the exception:
ArgumentException: Can not add Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JObject.
This occurs on the last line of my code below. "jSerializer.Serialize(o.CreateWriter(), o);"
JSON
{
"ArrayToManipulate":
[
{
"Name":"Value"
},
{
"Name":"value"
}
]
}
Code to manipulate the JSON Objects
JContainer o = (JObject)JToken.ReadFrom(new JsonTextReader(reader));
JArray x = (JArray)o["ArrayToManipulate"];
ContentObject newObject = new ContentObject(){Name="Value"};
JToken tokenToAdd = JToken.Parse(JsonConvert.SerializeObject(newObject, Formatting.Indented));
x.Add(tokenToAdd);
JsonSerializer jSerializer = new JsonSerializer();
jSerializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
jSerializer.Serialize(o.CreateWriter(), o);
Am I going about this the wrong way?