合并具有相同项目的JSON

时间:2018-08-24 09:02:23

标签: c# arrays json json.net

有没有一种方法可以合并这些JSON:

JSON 1

{
    "items": [{
        "date": "2018-08-25",
        "id": 1,
        "name": "John"
    }]
}

JSON 2

{
    "items": [{
        "date": "2018-08-25",
        "id": 1,
        "age": 20,
        "address": "USA",
        "Weight": "256 kg"
    }]
}

结果

{
    "items": [{
        "date": "2018-08-25",
        "id": 1,
        "name": "John",
        "age": 20,
        "address": "USA",
        "Weight": "256 kg"
    }]
}

请注意,我要合并ID与方位相同的JSON项。能做到吗?谢谢您的反馈

2 个答案:

答案 0 :(得分:3)

您可以尝试为json1和json2数据格式创建一些对象。

public class Item1
{
    public string date { get; set; }
    public int id { get; set; }
    public string name { get; set; }
}

public class Object1
{
    public List<Item1> items { get; set; }
}
public class Item2
{
    public string date { get; set; }
    public int id { get; set; }
    public int age { get; set; }
    public string address { get; set; }
    public string Weight { get; set; }
}

public class Object2
{
    public List<Item2> items { get; set; }
}
public class Item
{
    public string date { get; set; }
    public int id { get; set; }
    public string name { get; set; }
    public int age { get; set; }
    public string address { get; set; }
    public string Weight { get; set; }
}

public class Result
{
    public List<Item> items { get; set; }
}

并使用DeserializeObject从json字符串中获取对象数据。

Object1 obj1 = JsonConvert.DeserializeObject<Object1>(Json1);

Object2 obj2 = JsonConvert.DeserializeObject<Object2>(Json2);

然后使用linq join创建结果列表。

var resultList = (from o1 in obj1.items
join o2 in obj2.items on o1.id equals o2.id
select new Item()
{
    id = o1.id,
    date = o1.date,
    address = o2.address,
    age = o2.age,
     name = o1.name,
     Weight = o2.Weight
}).ToList();

最终使用SerializeObject变换Result生成json。

string jsonResult = JsonConvert.SerializeObject(new Result() { items = resultList });

编辑

如果要避免创建太多的类,可以尝试使用JObject.Parse join字典上的linq JToken,因此您无需创建对象类来携带JSON数据。

var obj1 = JObject.Parse(Json1);
var obj2 = JObject.Parse(Json2);

var resultList = (from o1 in obj1["items"]
                  join o2 in obj2["items"] on o1["id"] equals o2["id"]
                  select new Item()
                  {
                      id = o1["id"].ToObject<int>(),
                      date = o1["date"].ToObject<string>(),
                      address = o2["address"].ToObject<string>(),
                      age = o2["age"].ToObject<int>(),
                      name = o1["name"].ToObject<string>(),
                      Weight = o2["Weight"].ToObject<string>()
                  }).ToList();
JsonConvert.SerializeObject(new Result() { items = resultList });

string jsonResult = JsonConvert.SerializeObject(new Result() { items = resultList });

c# online

结果

{
    "items":[
        {
        "date":"2018-08-25",
        "id":1,
        "name":"John",
        "age":20,
        "address":"USA",
        "Weight":"256 kg"
        }
    ]
}

答案 1 :(得分:0)

{
    "date": "2018-08-25",
    "id": 1,
    "name": "John"
}

可以反序列化为Dictionary<string, object>