gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:37:09 27 4
gpt4 key购买 nike

我正在调用 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;
}

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

protected void onPostExecute(String result) {
super.onPostExecute(result);
TextView t1=(TextView)findViewById(R.id.textView1);
TextView t2=(TextView)findViewById(R.id.textView2);

//String s = result.replaceAll("\\", ""); //I have tried to escape backslashes also using this line.
String name="failure";
int id;
try
{
t1.setText(result);
JSONObject reader = new JSONObject(result.toString()); //Exception on this line.
//name = reader.getString("name").toString();
//id = reader.getInt("id");
t2.setText(name);
}
catch (Exception e)
{
t2.setText(e.toString());
}
}

响应字符串为:"{\"name\":\"Name\",\"id\":1}"当我尝试将其解析为 JSONObject 时,它会抛出异常: org.json.JSONException value "{"name":"Name","id":1}" type cannot conversion into json对象。

最佳答案

我知道这是一个老问题,但我遇到了同样的问题,唯一的区别是,我有一个 JSONArray 而不是 JSONObject 。但是,在 Stackoverflow 上搜索类似问题后,我所遵循的解决问题的方法就是我将在下面说明的方法,以防有人在我偶然发现你的问题时来寻找答案。

首先,您需要研究收到的 json 响应。 "{\"name\":\"Name\",\"id\":1}" 是一个字符串。您会在 { 之前和 } 之后看到 "。如果此字符串是您从网络服务收到的原始 json,那么您的转义方法反斜杠,然后从该字符串构建 JSONObject 应该可以工作。您可以尝试的一种方法是:

result = result.trim();
result = result.substring(1,result.length() - 1);
result = result.replace("\\","");

那么,

JSONObject reader = new JSONObject(result);
name = reader.getString("name");
id = reader.getInt("id");

此解决方案的要点是去掉 json 响应之前的那些额外的引号,然后从该字符串创建一个 JSONObject。

对于我关于 JSONArray 的情况,解决方案如下:

Json 响应:{"error":false,"myorderdetails":{"order_id":"26","orderjson":"[{\"Name\":\"Disprin\",\"ID\":53,\"数量\":1,\"价格\":50}]"}}

JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error)
{
JSONObject jsonObj= jObj.getJSONObject("myorderdetails");
String s = jsonObj.getString("orderjson");
s = s.replace("\\", "");
JSONArray orderjson = new JSONArray(s);

for(int i=0;i<orderjson.length();i++){
JSONObject jsonObject=orderjson.getJSONObject(i);
String pname = (String)jsonObject.get("Name");
int pquantity = jsonObject.getInt("Quantity");
double pprice = jsonObject.getDouble("Price");
}

关于java - JSON 响应字符串中包含自动添加的反斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27577937/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com