- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在调用 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/
我有一个右下角倾斜的元素,我必须在其上放置一个盒子阴影。有时倾斜的 Angular 被徽章覆盖 - 我的问题不适用,如果是这样的话: 这是信息框及其边 Angular 的 (s)css 部分(还有更多
是否可以在纯 html/css 中创建类似下面的内容? 我想做这个响应式和全 (100%) 宽度(最大左 Angular 100 像素,右边最小 50 像素,类似的东西)。 最佳答案 您可以通过转换(
如何在 fabricjs 文本中为文本提供渐变或斜 Angular 效果?? http://fabricjs.com/fabric-intro-part-2/ 这里给出了形状和所有示例,我将其与文本绑
我用过: http://apps.eky.hk/css-triangle-generator/ 为彼此对 Angular 放置的两个不等边三 Angular 形生成 css: 左下三 Angular
我是一名优秀的程序员,十分优秀!