A JSONObject text must begin with ‘{‘ at 1 [character 2 line 1]

今天调用一个接口,返回的是json数据,但是拿到数据进行转换的报错,

JSONObject resultJson = new JSONObject(resuStr);
报错信息是:
Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
at org.json.JSONObject.(JSONObject.java:195)
at org.json.JSONObject.(JSONObject.java:319)

解决方法:

int i = resuStr.indexOf("{");
resuStr = resuStr.substring(i);
JSONObject json = new JSONObject(resuStr.trim()); 
System.out.println(json.toString(4));  


原因是:

json文件头里带有编码字符(如UTF-8等),读取字符串时json串是正常的,但是解析就有异常

你可能感兴趣的:(python,开发语言)