【发布时间】:2021-03-21 18:41:21
【问题描述】:
我想从键值对映射 JSON 路径属性以在 C# 中生成 JSON 对象,其中路径包含嵌套数组索引路径
输入:
Dictionary<string, string> properties = new Dictionary<string, string>();
properties.put("id", "1");
properties.put("name", "sample_name");
properties.put("category.id", "1");
properties.put("category.name", "sample");
properties.put("tags[0].id", "1");
properties.put("tags[0].name", "tag1");
properties.put("tags[1].id", "2");
properties.put("tags[1].name", "tag2");
properties.put("status", "available");
输出:
{
"id": 1,
"name": "sample_name",
"category": {
"id": 1,
"name": "sample"
},
"tags": [
{
"id": 1,
"name": "tag1"
},
{
"id": 2,
"name": "tag2"
}
],
"status": "available"
}
使用Jackson's JavaPropsMapper 可以轻松实现:
JavaPropsMapper javaPropsMapper = new JavaPropsMapper();
JsonNode json = javaPropsMapper.readMapAs(properties, JsonNode.class);
如何在 C# 中实现这个想法,以便我能够从给定的 JSON 路径节点生成 JSON 对象。
【问题讨论】:
-
你使用的是哪个 JSON 序列化器?
-
我正在使用 Newtonsoft JSON 序列化程序
标签: c# arrays json json.net json-path-expression