今天调用一个接口,接收的是json字符串,但是拿到数据进行转换的报错,JSONObject json1 = new JSONObject(personal);
接收到的字符串是下面的样子:
personal格式是"{\"sellerCode\":\"某某某某\",\"status\":true}"
而真正需要的是{"sellerCode":"某某某某","status":true},因为json的格式不对才造成的错误。
解决方法:
int i = personal.indexOf("{");
personal = personal.substring(i);
int p = personal.lastIndexOf("}");
personal = personal.substring(i-1,p+1);
personal = personal.replaceAll("\\\\","");
org.json.JSONObject json1 = new org.json.JSONObject(personal.trim());
转自:https://blog.csdn.net/nicolewjt/article/details/87180153