我有来自web服务的数据,如下所示:
"[{\"ID\":9617,\"Text\":\"1 1\/4\\\" PVC\/GI CLAMPS\"},{\"ID\":41608,\"Text\":\"100123-Manterial name\"}]"
现在的问题是我想将这个json数据解析为json数组但由于字符串中的双引号(")而导致错误异常,我尝试手动删除反斜杠。我使用这样的代码将其转换为json数组。
String line="[{\"ID\":9617,\"Text\":\"1 1\/4\\\" PVC\/GI CLAMPS\"},{\"ID\":41608,\"Text\":\"100123-Manterial name\"}]";
String jsonFormattedString = line.replace("\\\\", "");
jsonFormattedString= jsonFormattedString.substring(1, jsonFormattedString.length()-1) ;
Log.d("Json String--->",jsonFormattedString);
JSONArray j = new JSONArray(jsonFormattedString);//here I get exception cannot covert to json array
JSONObject jsonmain = j.getJSONObject(0);
int lengthJsonArr = j.length();
for(int i=0; i < lengthJsonArr; i++) {
jsonmain = j.getJSONObject(i);
Log.d("Jsonnnnn----",jsonmain.getString("Text"));
Itm=new CItem(jsonmain.getString("ID"),jsonmain.getString("Text"));
lstItm.add(Itm);
}
答案 0 :(得分:0)
你试过这个吗?
String jsonFormattedString = line.replaceAll("\\\\", "");
答案 1 :(得分:0)
试试这个方法
public String convertStandardJSONString(String data_json){
data_json = data_json.replace("\\", "");
data_json = data_json.replace("\"{", "{");
data_json = data_json.replace("}\",", "},");
data_json = data_json.replace("}\"", "}");
return data_json;
}
@REF:Cristy YG
答案 2 :(得分:0)
public static String convertStandardJSONString(String data_json) {
data_json = data_json.replaceAll("\\\\r\\\\n", "");
data_json = data_json.replace("\"{", "{");
data_json = data_json.replace("}\",", "},");
data_json = data_json.replace("}\"", "}");
return data_json;
}
convertStandardJSONString
)。