Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine/merge two JArrays in JSON.NET

Tags:

json

c#

json.net

I can't figure out how to concatenate two JArrays that I got by using JArray.Parse? The order of the arrays must be preserved i.e. the first array should be first and element in seconds should come afterwards.

like image 644
tunafish24 Avatar asked Mar 12 '13 20:03

tunafish24


People also ask

How to merge two JSON c#?

You can use JObject. Merge to merge two different objects into one: JObject o1 = JObject. Parse(@"{ 'FirstName': 'John', 'LastName': 'Smith', 'Enabled': false, 'Roles': [ 'User' ] }"); JObject o2 = JObject.

How to merge two JSON arrays in c#?

To do so, we simply need to call the static Parse method from the JObject class, passing as input the JSON string. As output, this method returns the parsed JObject. var jObject1 = JObject. Parse(jsonString1);

How do I combine two JSON objects with the same key?

Use concat() for arrays Assuming that you would like to merge two JSON arrays like below: var json1 = [{id:1, name: 'xxx' ...}] var json2 = [{id:2, name: 'xyz' ...}]


1 Answers

I used the Merge method, which modifies the original JArray:

 JArray test1 = JArray.Parse("[\"john\"]");
 JArray test2 = JArray.Parse("[\"doe\"]");
 test1.Merge(test2);
like image 67
Aaron Avatar answered Oct 22 '22 14:10

Aaron