0

我正在准备 JSON 和我做的一些部分

ls += "{ \"id\": \"" + list.indexOf(el) + "\", \"cell\" : [\"" +  el.toString() + "\"]}," ;

但是当我检查 Firebug 发送的内容时,我看到没有删除反斜杠:

{"total":"1","page":"1","records":"1","rows":"[{ \"id\": \"0\", \"cell\" : [\"data1\"]},{ \"id\": \"1\", \"cell\" : [\"data2\"]}]"}

有任何想法吗?我试过了,ls.replaceAll("\\\\", " ");但它不起作用

4

1 回答 1

0

What JSON library are you using? It looks to me like you're correctly building strings like

{ "id": "0", "cell" : ["data1"]}

but then inserting them as strings into some structure that is serialized by a JSON library. The JSON serializer then has to re-escape the quotes to make the result into a valid JSON string literal.

Edit: you say you're using Gson, so I presume you have a top level class you're serializing which currently has a String rows property which you're filling with a JSON string. Instead you need to create a new class to represent one row

class Row {
  private String id;
  private List<String> cell;
  // constructor, getters and setters as usual
}

And change the rows property to be a List<Row>. Now rather than concatenating strings together to build the JSON yourself you simply populate your top-level object with the appropriate Row objects and let Gson handle converting them to JSON.

于 2013-03-29T22:23:20.747 回答