我有 list
Gson gson = new Gson();
List<String> exampleList = new ArrayList<String>();
exampleList.add("aaa");
exampleList.add("bbb");
exampleList.add("ccc");
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("test", gson.toJson(exampleList));
而 jsonObject 是 {"test":"[\"aaa\",\"bbb\",\"ccc\"]"}
但我需要得到关注 {“测试”:[“aaa”,“bbb”,“ccc”]}
有什么方法可以做到这一点?
replaceAll 在几个方面都没有解决这个问题
最佳答案
您正在添加一个键值映射 String -> String
,这就是引号被转义的原因(事实上,您的值是由 toString 给出的列表的字符串表示形式()
方法)。如果您想要映射 String -> Array
,您需要将列表转换为 JsonArray
并将其添加为属性。
jsonObject.add("test", gson.toJsonTree(exampleList, new TypeToken<List<String>>(){}.getType()));
关于java - 从字符串(gson)的显示中删除反斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34201080/