c# - 如何正确合并 2 个 JSON
问题描述
我有 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"
}
]
}
解决方案
如果您的项目允许您为收到的 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 文件内容提取到它们各自的 List 类型中
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);
然后基于两个列表对两个列表执行 LINQ Join 操作instanceUid
// 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
};
推荐阅读
- wix - 复制日志无法复制 Wix 安装程序中的完整信息
- gradle - 应用用 Groovy 编写的自定义插件
- ios - 在滚动视图中设置具有动态大小的多个视图
- regex - Notepad++ 正则表达式剪切行的最后一个单词并将其粘贴到开始
- r - R ggplot2 构面内的构面
- ruby-on-rails - 如何在 Rail 5 中使用 Materialize 和 SimpleForm 将 CSS 类应用于 Select
- c# - 尝试在几次中仅读取一次值
- c - Xlib解析颜色不渲染
- java - 我们有专门针对 Spring 5.x 的 XSD 吗?
- python - 最简单的 Python 多线程 - 编写一个简单的文件