无法将 Java 字符串转换为 JSON

Mil*_*los 3 java android json

我有从服务器获取的 JSON:

 "[{\"id\":\"1\",\"name\":\"Milos\",\"city\":\"Smederevo\",\"email\":\"milos\",\"password\":\"\"},
 {\"id\":\"3\",\"name\":\"Boban\",\"city\":\"Beograd\",\"email\":\"bole\",\"password\":\"\"},
 {\"id\":\"4\",\"name\":\"Pele\",\"city\":\"Brazil\",\"email\":\"pele@pele.com\",\"password\":\"\"},
 {\"id\":\"5\",\"name\":\"admin\",\"city\":\"Smederevo\",\"email\":\"admin\",\"password\":\"\"}]"
Run Code Online (Sandbox Code Playgroud)

我正在使用该 json 并发送到我的线程(android 线程):

  try {
            // Method from which I am getting Json described above
            String s = dm.getAllUsers();

            /*JSONParser jp = new JSONParser();             
            JsonElement jelement = new JsonParser().parse(s);   
                JsonArray array1 = jelement.getAsJsonArray();*/

            JSONArray array = new JSONArray(s);
            for (int i = 0; i < array.length(); i++) {

                 JSONObject menuObject = array.getJSONObject(i);

                 // doing something with the object
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

我根本无法处理那个 Json。我收到错误“java.lang.String 无法转换为 JSONArray”。A 知道问题是由“\”引起的,我只是不知道如何摆脱“\”。

我试过:

1) s.replace("\\", "");
2) s.replace("\"", "'");
3) s.replaceAll("\\", "");
4) s.replaceAll("\"", "'");
Run Code Online (Sandbox Code Playgroud)

为了擦除“\”但替换根本没有反应。我还尝试使用“google-gson-2.2.2”库解决问题(上面注释下的代码,方法下)。

请问有什么建议吗?

Pra*_*rge 5

试试这个解决方案。

s.replaceAll("\\\\", "");
Run Code Online (Sandbox Code Playgroud)

这肯定会奏效。