Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add string array to a JObject

Tags:

json

c#

json.net

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?

like image 693
John webner Avatar asked Oct 18 '25 07:10

John webner


1 Answers

Try this way:

JObject obj = new JObject();
obj.Add("item1", JToken.FromObject(new[] { "a", "b" }));
like image 129
Stringfellow Avatar answered Oct 20 '25 21:10

Stringfellow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!