【发布时间】:2016-10-23 14:27:22
【问题描述】:
我正在从属性文件中加载一个值,然后将其传递给 gson 方法以将其转换为最终的 json 对象。但是,来自属性文件的值具有双引号,gson 正在将“\”添加到输出中。我已经扫描了整个网络,但找不到解决方案
属性文件包含
0110= This is a test for the renewal and the "Renewal no:"
这是我的代码
public String toJSONString(Object object) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
//Note object here is the value from the property file
return gson.toJson(object);
}
这会产生
"{ResponseCode:0110,ResponseText:This is a test for the renewal and the \"Renewal no:\"}"
我不确定在输出中,为什么它在文字周围添加或包装 \ 或者我们在属性文件值中的哪里有双引号?
【问题讨论】:
-
你的输出实际上不是 JSON,只是一个字符串。
-
@cricket_007 这是一个 JSON 字符串。
-
@SotiriosDelimanolis 我的意思是,例如,
ResponseCode是一个键,不是吗?所以,它应该被引用。 -
请说明您是如何打电话给
toJSONString的。 -
@cricket_007 在他们澄清之前,我已经删除了我的答案,但生成的 JSON 是有效的 JSON 字符串。我不想猜测他们传递给
toJSONString的内容。