小编Jah*_*han的帖子

在 JSON 响应字符串中包含自动添加的反斜杠

我正在调用 Web 服务以获取 JSON 字符串响应,它包含不在原始字符串中的反斜杠。下面是我请求一个 JSON 字符串对象的代码,它是: {"name":"Name","id":1}

protected String doInBackground(String... uri) 
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        return responseString;
    }
Run Code Online (Sandbox Code Playgroud)

在执行后,我只是尝试将此响应字符串解析为 JSONObject。代码如下:

protected …
Run Code Online (Sandbox Code Playgroud)

java string json backslash jsonexception

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

backslash ×1

java ×1

json ×1

jsonexception ×1

string ×1