如何正确合并 2 个 JSON

时间:2021-06-11 16:13:57

标签: c# json.net

我有 2 个从单独的 API 调用中获得的 JSON。

这是第一个:

{
   [
      {
         "name":"job a",
         "instanceUid":"c083aa59-b362-400b-a4e1-245ce707d3a9",
         "type":"BackupCopy",
         "status":"Success",
         "lastRun":"2020-10-20T11:53:10.183-04:00"
      },
      {
         "name":"job b",
         "instanceUid":"b65d3326-1878-4a4b-858c-522806da172f",
         "type":"BackupCopy",
         "status":"Success",
         "lastRun":null
      },
      {
         "name":"job c",
         "instanceUid":"02bef7df-08a8-4d75-a50e-3dc7e8dd9edb",
         "type":"BackupVm",
         "status":"Failed",
         "lastRun":"2021-02-24T17:40:09.23-05:00"
      },
      {
         "name":"job d",
         "instanceUid":"f1848f1f-962d-49ba-a0b0-cac9c1d5746a",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2020-08-11T13:52:09.987-04:00"
      },
      {
         "name":"job e",
         "instanceUid":"e2ac93da-3028-4501-91bd-1e25255651eb",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2021-02-24T16:00:22.733-05:00"
      },
      {
         "name":"job f",
         "instanceUid":"db81e5c5-4e6f-4160-bced-f26f85a6cea2",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2021-02-25T13:00:01.663-05:00"
      },
      {
         "name":"job g",
         "instanceUid":"bd45a460-1af3-4bfe-aa07-02b959487bdd",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2021-02-24T17:00:03.253-05:00"
      },
      {
         "name":"job h",
         "instanceUid":"a4f5c4d8-c84b-4efd-91f0-cf8429604d6f",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2021-02-24T17:00:04.393-05:00"
      },
      {
         "name":"job i",
         "instanceUid":"9623dda4-8d8d-45b5-aa8a-b59feb828178",
         "type":"BackupVm",
         "status":"Failed",
         "lastRun":"2021-02-24T17:30:54.88-05:00"
      },
      {
         "name":"job j",
         "instanceUid":"8e8319d6-a03e-4926-969f-c38f060b2bb1",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2021-02-24T17:00:03.643-05:00"
      },
      {
         "name":"job k",
         "instanceUid":"79840a7e-b4b1-4284-ba20-de4eef1ada3f",
         "type":"BackupVm",
         "status":"Failed",
         "lastRun":"2021-02-24T17:30:54.893-05:00"
      },
      {
         "name":"job l",
         "instanceUid":"32e5569f-3d7c-4bff-9fbf-cf00a9efddcf",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2021-02-24T17:00:04.02-05:00"
      },
      {
         "name":"job m",
         "instanceUid":"1230f103-38ce-405b-88c6-6f5c0cd62119",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2021-02-24T21:00:19.123-05:00"
      }
   ]
}

第二个:

{
   [
      {
         "instanceUid":"02bef7df-08a8-4d75-a50e-3dc7e8dd9edb",
         "protectedVmCount":0
      },
      {
         "instanceUid":"f1848f1f-962d-49ba-a0b0-cac9c1d5746a",
         "protectedVmCount":1
      },
      {
         "instanceUid":"e2ac93da-3028-4501-91bd-1e25255651eb",
         "protectedVmCount":1
      },
      {
         "instanceUid":"db81e5c5-4e6f-4160-bced-f26f85a6cea2",
         "protectedVmCount":0
      },
      {
         "instanceUid":"bd45a460-1af3-4bfe-aa07-02b959487bdd",
         "protectedVmCount":1
      },
      {
         "instanceUid":"a4f5c4d8-c84b-4efd-91f0-cf8429604d6f",
         "protectedVmCount":1
      },
      {
         "instanceUid":"9623dda4-8d8d-45b5-aa8a-b59feb828178",
         "protectedVmCount":1
      },
      {
         "instanceUid":"8e8319d6-a03e-4926-969f-c38f060b2bb1",
         "protectedVmCount":1
      },
      {
         "instanceUid":"79840a7e-b4b1-4284-ba20-de4eef1ada3f",
         "protectedVmCount":1
      },
      {
         "instanceUid":"32e5569f-3d7c-4bff-9fbf-cf00a9efddcf",
         "protectedVmCount":2
      },
      {
         "instanceUid":"1230f103-38ce-405b-88c6-6f5c0cd62119",
         "protectedVmCount":1
      }
   ]
}

我使用以下 C# 代码使用 Newtonsoft JSON 库尝试合并 2 并尝试将其基于 instanceUid 作为键。 (我做了 SelectToken 因为原始 json 有一个我不需要的元数据部分。)

var a = (JArray)organizationResponse.SelectToken("data");
var b = (JArray)protectedVms.SelectToken("data");

a.Merge(b, new JsonMergeSettings
{
    MergeArrayHandling = MergeArrayHandling.Merge
    
});

但我得到以下结果。一些 instanceUid 放置错误,最后 2 个作业 j 和 k 没有 protectedVMCount 属性。 Newtonsoft 文档提到 MergeArrayHandling.Merge 基于索引进行合并,但他们没有提供有关此“索引”的示例。有谁知道怎么做或者有其他方法吗?基本上我正在尝试基于 instanceUid 进行合并。谢谢。

{
   [
      {
         "name":"job a",
         "instanceUid":"02bef7df-08a8-4d75-a50e-3dc7e8dd9edb",
         "type":"BackupCopy",
         "status":"Success",
         "lastRun":"2020-10-20T11:53:10.183-04:00",
         "protectedVmCount":0
      },
      {
         "name":"job b",
         "instanceUid":"f1848f1f-962d-49ba-a0b0-cac9c1d5746a",
         "type":"BackupCopy",
         "status":"Success",
         "lastRun":null,
         "protectedVmCount":1
      },
      {
         "name":"job c",
         "instanceUid":"e2ac93da-3028-4501-91bd-1e25255651eb",
         "type":"BackupVm",
         "status":"Failed",
         "lastRun":"2021-02-24T17:40:09.23-05:00",
         "protectedVmCount":1
      },
      {
         "name":"job d",
         "instanceUid":"db81e5c5-4e6f-4160-bced-f26f85a6cea2",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2020-08-11T13:52:09.987-04:00",
         "protectedVmCount":0
      },
      {
         "name":"job e",
         "instanceUid":"bd45a460-1af3-4bfe-aa07-02b959487bdd",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2021-02-24T16:00:22.733-05:00",
         "protectedVmCount":1
      },
      {
         "name":"job f",
         "instanceUid":"a4f5c4d8-c84b-4efd-91f0-cf8429604d6f",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2021-02-25T13:00:01.663-05:00",
         "protectedVmCount":1
      },
      {
         "name":"job g",
         "instanceUid":"9623dda4-8d8d-45b5-aa8a-b59feb828178",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2021-02-24T17:00:03.253-05:00",
         "protectedVmCount":1
      },
      {
         "name":"job f",
         "instanceUid":"8e8319d6-a03e-4926-969f-c38f060b2bb1",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2021-02-24T17:00:04.393-05:00",
         "protectedVmCount":1
      },
      {
         "name":"job g",
         "instanceUid":"79840a7e-b4b1-4284-ba20-de4eef1ada3f",
         "type":"BackupVm",
         "status":"Failed",
         "lastRun":"2021-02-24T17:30:54.88-05:00",
         "protectedVmCount":1
      },
      {
         "name":"job h",
         "instanceUid":"32e5569f-3d7c-4bff-9fbf-cf00a9efddcf",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2021-02-24T17:00:03.643-05:00",
         "protectedVmCount":2
      },
      {
         "name":"job i",
         "instanceUid":"1230f103-38ce-405b-88c6-6f5c0cd62119",
         "type":"BackupVm",
         "status":"Failed",
         "lastRun":"2021-02-24T17:30:54.893-05:00",
         "protectedVmCount":1
      },
      {
         "name":"job j",
         "instanceUid":"32e5569f-3d7c-4bff-9fbf-cf00a9efddcf",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2021-02-24T17:00:04.02-05:00"
      },
      {
         "name":"job k",
         "instanceUid":"1230f103-38ce-405b-88c6-6f5c0cd62119",
         "type":"BackupVm",
         "status":"Success",
         "lastRun":"2021-02-24T21:00:19.123-05:00"
      }
   ]
}

1 个答案:

答案 0 :(得分:0)

试试这个代码,如果你的项目允许你为你收到的 JSON 创建类。

首先创建这些类

public class ORGType
{
    public string name { get; set; }
    public string instanceUid { get; set; }
    public string type { get; set; }
    public string status { get; set; }
    public string lastRun { get; set; }
}

public class protectecVMS
{
    public string instanceUid { get; set; }
    public string protectedVmCount { get; set; }
}

然后将 JSON 文件内容提取到它们各自的列表类型中

string path = "./organtisationResponse.json";
string orgJSONFilecontent = File.ReadAllText(path);

var orgobjList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ORGType>>(orgJSONFilecontent);

path = "./protectedvms.json";
string protectedJsonFilecontent = File.ReadAllText(path);

var protectedobjList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<protectecVMS>>(protectedJsonFilecontent);

然后根据 instanceUid

对两个列表执行 LINQ Join 操作
// join to list based oninstance uid, 
var result = orgobjList.Join
    (
        protectedobjList,
        a => a.instanceUid,
        b => b.instanceUid,
        (a, b) => new { A = a, B = b })
    .Where(c => c.A.instanceUid == c.B.instanceUid)
    .Select(i => new { i.A.instanceUid, i.A.lastRun, i.A.name, i.A.status, i.A.type, i.B.protectedVmCount });

var resultJSON = Newtonsoft.Json.JsonConvert.SerializeObject(result);

为了检查protectedObjList中是否存在相应的元素,试试这个代码。

result = from orgObj in orgobjList
         join protectedObj in protectedobjList on orgObj.instanceUid equals protectedObj.instanceUid into gj
         from subProtectedObj in gj.DefaultIfEmpty()
         select new
         {
             orgObj.instanceUid,
             orgObj.lastRun,
             orgObj.name,
             orgObj.status,
             orgObj.type,
             protectedVmCount = subProtectedObj?.protectedVmCount ?? string.Empty
         };
相关问题