【发布时间】:2022-11-24 14:45:24
【问题描述】:
我有以下 unstructured 但有效的 JSON 需要使用任何 C# library 或 newtonsoft- 转换为 structured 格式
{
"root_id": {
"Path": "InsertCases",
"MainContract": "CreateCaseParameter"
},
"root_tittel": {
"Path": "InsertCases",
"MainContract": "CreateCaseParameter"
},
"root_mottaker_adresse1": {
"Path": "InsertDocuments",
"MainContract": "CreateDocumentParameter"
},
"root_mottaker_adresse2": {
"Path": "InsertCases",
"MainContract": "CreateCaseParameter"
},
"root_web_id_guid": {
"Path": "InsertCases",
"MainContract": "CreateCaseParameter"
}
}
想让它的结构如下 -
{
"id": {
"Path": "InsertCases",
"MainContract": "CreateCaseParameter"
},
"tittel": {
"Path": "InsertCases",
"MainContract": "CreateCaseParameter"
},
"mottaker": {
"adresse1": {
"Path": "InsertDocuments",
"MainContract": "CreateDocumentParameter"
},
"adresse2": {
"Path": "InsertCases",
"MainContract": "CreateCaseParameter"
}
},
"web": {
"id": {
"guid": {
"Path": "InsertCases",
"MainContract": "CreateCaseParameter"
}
}
}
}
如果您看到差异,层次结构将被 _(下划线)分割。我想以更嵌套的方式制作它。
IE。
- root_element -> 元素
- root_element1_element2 -> 元素 1 是父元素,元素 2 是子元素。
提前致谢!
【问题讨论】:
-
好吧,这似乎很清楚你想做什么。在我看来,它有两个主要方面:“理解”原始输入,以及在进行过程中构建输出。目前哪部分有困难?你能展示你有多远吗?
-
@JonSkeet 在答案中发布了我的代码,以防万一您认为这可以通过任何其他优化方式完成。请回答。
标签: c# json .net json.net nested-json