I tried
JObject obj = new JObject();
obj["item1"] = new string[] {"a","b"};
and got
Cannot implicitly convert type 'string[]' to 'Newtonsoft.Json.Linq.JToken'
How do I add a string array to a JObject? Also, why do I get that error?
Try this way:
JObject obj = new JObject();
obj.Add("item1", JToken.FromObject(new[] { "a", "b" }));
JToken can be an object or an array, a JArray can only be an arrayobj.Add("item1", JToken.FromObject(new[] { "a", "b" })); converts the JToken into a JArray. obj["item1"].GetType(); results in {Name = "JArray" FullName = "Newtonsoft.Json.Linq.JArray"}