-1

キーと値のペアから 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 の JavaPropsMapperを使用すると、次のように簡単に実現できます。

JavaPropsMapper javaPropsMapper = new JavaPropsMapper();
JsonNode json = javaPropsMapper.readMapAs(properties, JsonNode.class);

指定された JSON パス ノードから JSON オブジェクトを生成できるように、 C#でこのアイデアを実装する方法。

4

1 に答える 1