我们可以利用Gson包将String类型的JSON数据进行格式化。

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(uglyJSONString);
String prettyJsonString = gson.toJson(je);

使用 new GsonBuilder.setPrettyPrinting().create()方法创建的Gson对象来生成的JSON数据就是格式化的数据,上面的转换代码只是针对原料是JSON String的情况。

从stackoverflow上抄过来的。

相关文章:

  • 2021-11-18
  • 2021-12-22
  • 2021-11-28
  • 2022-12-23
  • 2021-11-18
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-21
  • 2021-11-08
  • 2022-12-23
  • 2021-12-20
  • 2021-12-10
相关资源
相似解决方案