How can I merge two JObject? [duplicate]

2020-01-26 09:33发布

I have a first json:

{
    "data": [{
      "id": "id1",
      "field": "field1"
    }],
    "paging": {
        "prev": "link1",
    }
}

and a second one:

{
    "data": [{
      "id": "id2",
      "field": "field2"
    }],
    "paging": {
        "prev": "link2",
    }
}

and I want to merge/union the two Data array, such as:

{
    "data": [{
      "id": "id1",
      "field": "field1"
    },
    {
      "id": "id2",
      "field": "field2"
    }]  
}

(I don't care about about paging right now).

How can I do it quick and easy? This is my try:

var final = JsonConvert.SerializeObject(new { data = json1["data"].Union(json2["data"]) }, Newtonsoft.Json.Formatting.Indented).ToString();

but an Exception is raised: 'Newtonsoft.Json.Linq.JArray' does not contains a definition of 'Union'

4条回答
Melony?
2楼-- · 2020-01-26 10:01
JArray dataOfJson1=json1.SelectToken("data");

JArray dataofJson2=json2.SelectToken("data");

foreach(JObject innerData in dataofJson2) 
{
    dataOfJson1.Add(innerData);
}
查看更多
家丑人穷心不美
3楼-- · 2020-01-26 10:07

A possible solution could be:

class Container
{
    public List<IdField> data{get;set;}
}

class IdField
{
    public string id{get;set;}
    public string field{get;set;}
}


string s1 = "{ \"data\": [{ \"id\": \"id1\", \"field\": \"field1\" }], \"paging\": { \"prev\": \"link1\", } }";
string s2 = "{ \"data\": [{ \"id\": \"id2\", \"field\": \"field2\" }], \"paging\": { \"prev\": \"link2\", } }";

var d1 = JsonConvert.DeserializeObject<Container>(s1);
var d2 = JsonConvert.DeserializeObject<Container>(s2);

d1.data.AddRange(d2.data);

var result = JsonConvert.SerializeObject(d1);
查看更多
SAY GOODBYE
4楼-- · 2020-01-26 10:16

For those that (like me) cannot use the new JSON.net library. The following method is what I use.

public static JObject mergeJsonObjects(List<JObject> objects) {

    JObject json = new JObject();
    foreach(JObject JSONObject in objects) {
        foreach(var property in JSONObject) {
            string name = property.Key;
            JToken value = property.Value;

            json.Add(property.Key, property.Value);
        }
    }

    return json;
}

The method takes an list of JObjects and returns a single JObject, simpel and effective.

查看更多
干净又极端
5楼-- · 2020-01-26 10:23

Newtonsoft.Json now supports merging objects:
http://antix.co.uk/Blog/Merging-objects-using-JSON.NET

var dataObject1 = JObject.Parse(@"{
    ""data"": [{
        ""id"": ""id1"",
        ""field"": ""field1""
    }],
    ""paging"": {
        ""prev"": ""link1"",
    }
}");
var dataObject2 = JObject.Parse(@"{
    ""data"": [{
        ""id"": ""id2"",
        ""field"": ""field2""
    }],
    ""paging"": {
        ""prev"": ""link2"",
    }
}");

var mergeSettings = new JsonMergeSettings
{
    MergeArrayHandling = MergeArrayHandling.Union
};

// method 1
(dataObject1.SelectToken("data") as JArray).Merge(dataObject2.SelectToken("data"), mergeSettings);
// method 2
//dataObject1.Merge(dataObject2, mergeSettings);

var mergedArray = dataObject1.SelectToken("data") as JArray;

Console.WriteLine(mergedArray.ToString(Formatting.None));

(checked with brain-compiler ;) )

查看更多
登录 后发表回答