首页 前端知识 A JSONObject text must begin with ‘{‘ at 1 [character 2 line 1]

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

2024-07-08 09:07:24 前端知识 前端哥 364 680 我要收藏

今天调用一个接口,返回的是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.<init>(JSONObject.java:195)
at org.json.JSONObject.<init>(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串是正常的,但是解析就有异常

转载请注明出处或者链接地址:https://www.qianduange.cn//article/13903.html
标签
评论
发布的文章

html左右两栏布局实现

2024-08-04 00:08:50

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!