将Json转换为数据集或列表
本文关键字:列表 数据集 Json 转换 | 更新日期: 2023-09-27 18:27:06
我想将json转换为一个列表或数据集m,从REST API获得如下格式的json
)]}'
{
"DASH/er/manifest": {
"kind": "gerritcodereview#project",
"id": "DASH%2Fer%2Fmanifest",
"state": "ACTIVE"
},
"DASH/er/projects/amc": {
"kind": "gerritcodereview#project",
"id": "DASH%2Fer%2Fprojects%2Famc",
"state": "ACTIVE"
},
"DASH/er/projects/dash-config": {
"kind": "gerritcodereview#project",
"id": "DASH%2Fer%2Fprojects%2Fdash-config",
"state": "ACTIVE"
},
"DASH/er/projects/dash-sccm-plugin": {
"kind": "gerritcodereview#project",
"id": "DASH%2Fer%2Fprojects%2Fdash-sccm-plugin",
"state": "ACTIVE"
},
"DASH/er/projects/dash-sdk": {
"kind": "gerritcodereview#project",
"id": "DASH%2Fer%2Fprojects%2Fdash-sdk",
"state": "ACTIVE"
},
"DASH/er/source/dash-client-sdk": {
"kind": "gerritcodereview#project",
"id": "DASH%2Fer%2Fsource%2Fdash-client-sdk",
"state": "ACTIVE"
},
"DASH/er/source/dash-configmgr-plugin": {
"kind": "gerritcodereview#project",
"id": "DASH%2Fer%2Fsource%2Fdash-configmgr-plugin",
"state": "ACTIVE"
},
"DASH/er/source/dash-manifests": {
"kind": "gerritcodereview#project",
"id": "DASH%2Fer%2Fsource%2Fdash-manifests",
"state": "ACTIVE"
}
}
有人能帮我把这个json转换成数据集或列表吗?谢谢
确保将JSON.NET作为NuGet包安装到应用程序中。
首先创建一个具有匹配属性(如Kind、Id和State)的模型类。
var myList = JsonConvert.DeserializeObject<List<YourClass>>(jsonString);
这将为您提供一个JSON字符串的对象列表。
此外,JSON的格式化方式会有点困难,因为最高级别每次都是唯一的。如果你可以控制JSON的格式,我会让它成为所有项目的通用类,比如下面的例子中的Post:
{
"Post": {
"url": "DASH/er/manifest"
"kind": "gerritcodereview#project",
"id": "DASH%2Fer%2Fmanifest",
"state": "ACTIVE"
},
...
}