I have a very simple piece of code that I just cannot work out.
JObject obj = new JObject { "Name", "John" };
JArray array = new JArray();
array.Add(obj);
// throws "Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject"
I have tried many different things to add a JObject
to a JArray
. What am I doing wrong?
Your problem is not the adding part.
Your problem is the initialization of your JOject
.
Try this.
JObject obj = new JObject();
obj.Add("Name", "John");
Jarray array = new JArray();
array.Add(obj);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With