C# 如何在C中从JSON节点路径创建JSON对象#
我想从键值对映射JSON路径属性,以在C#中生成JSON对象,其中路径包含嵌套数组索引路径 输入:C# 如何在C中从JSON节点路径创建JSON对象#,c#,arrays,json,json.net,json-path-expression,C#,Arrays,Json,Json.net,Json Path Expression,我想从键值对映射JSON路径属性,以在C#中生成JSON对象,其中路径包含嵌套数组索引路径 输入: Dictionary<string, string> properties = new Dictionary<string, string>(); properties.put("id", "1"); properties.put("name", "sample_name"); properti
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");
使用它可以很容易地实现,如:
JavaPropsMapper javaPropsMapper = new JavaPropsMapper();
JsonNode json = javaPropsMapper.readMapAs(properties, JsonNode.class);
如何在C#中实现这个想法,以便我能够从给定的JSON路径节点生成JSON对象。您可以创建一个序列化的匿名对象
var values = new {
id = "id",
name = "name",
category = new { id = 1, name = "sample"},
tags = new { id = 0, name = "sample" },
status = "available"
};
string json = JsonConvert.SerializeObject(values);
您使用的是哪个JSON serialiser?我使用的是Newtonsoft JSON Serializer类似的问题:首先,如何解析字典中的给定表达式,例如标记[0].id。我的输入本质上是动态的。我可以得到生成JSON节点所需的任何类型的表达式
var values = new {
id = "id",
name = "name",
category = new { id = 1, name = "sample"},
tags = new { id = 0, name = "sample" },
status = "available"
};
string json = JsonConvert.SerializeObject(values);