json 如何将对象数组转换为Springapplication.properties

tuwxkamq  于 2023-01-14  发布在  Spring
关注(0)|答案(1)|浏览(173)

我正在使用Jackson转换对象,包括数组对象到www.example.com文件,但转换数组对象时的结果看起来不像我预期的那样好。application.properties file but the result when convert array object look not good as I expected. This is my code:

JSONSerializer serializer = new JSONSerializer().rootName("application").prettyPrint(false);
 String json = serializer.serialize(configDto.getValue());

 ObjectMapper om = new ObjectMapper();
 JsonNode tree = om.readTree(json);
 Properties props;
 JavaPropsMapper mapper = new JavaPropsMapper();

 props = mapper.writeValueAsProperties(tree);
 props.store(new FileOutputStream(file), "");

输入:

{
  "nation": [
      {
        "key": "France"
      },
      {
        "key": "Spain"
      },
      {
        "key": "England"
      }
    ],
    "movie": "Avatar 2"
}

结果:

application.nation.1 = France
application.nation.2 = Spain
application.nation.3 = England
application.movie = Avatar 2

预期结果:

application.nation[1] = France
application.nation[2] = Spain
application.nation[3] = England
application.movie = Avatar 2

我怎样才能把数据转换成我期望的结果呢?对不起,我的英语不好!谢谢!

rqqzpn5f

rqqzpn5f1#

可能会在json数组中生成nation。对不起,我还没有测试过它。

{
  "nation": ["France", "Spain", "England"],
  "movie": "Avatar 2"
}

相关问题