如何摆脱JSON密钥中的\字符(java)

时间:2015-04-21 12:03:14

标签: java android json eclipse escaping

在你投票之前,问题就在于它。我正在为我的客户做点什么,我必须满足他的要求,即使他们不遵守最佳实践。我相信你有一段时间处于这种状况,你明白了。

我在Eclipse(Android应用程序)中填充JSONObject:

jtappedRow="{"label":"red","key":"3"}";
jactionresult.put("value",jtappedRow);
jactionresult.put( bla bla bla..... )

但是,当在LOG上打印jactionresult时,就像这样:

Log.e(tag,"TAP THIS TO ENGINE!: "+jactionresult.toString());

我明白了:

TAP THIS TO ENGINE!: {"value":"{\"label\":\"green\",\"key\":\"1\"}","result":"success","action":"displayClickableList"}

如何摆脱那些\字符???我需要的结果就是:

TAP THIS TO ENGINE!: {"value":"`{"label":"green","key":"1"}","result":"success","action":"displayClickableList"}`

3 个答案:

答案 0 :(得分:0)

假设您有发布的字符串,可以使用String#replace

String res = jsonStr.replace("\\\"", "\"");

答案 1 :(得分:0)

字符串替换或使用像jackson或gson这样的框架进行解析

答案 2 :(得分:0)

如果您获得反斜杠,则表示它被读取为字符串。使用以下代码将其转换为JSONObject:

JSONObject jtappedRow = new JSONObject();
jtappedRow.put("label", "red");
jtappedRow.put("key", "3");
jactionresult.put("value",jtappedRow);
jactionresult.put( bla bla bla..... )

或者你可以做这样的事情

jtappedRow={"label":"red","key":"3"};
JSONObject json = new JSONObject(jtappedRow);
jactionresult.put("value", json);
jactionresult.put( bla bla bla..... )