报错:JSONException: illegal identifier

简介: 报错:JSONException: illegal identifier : \pos 1, line 1, column 2 或JSONException: not close json text, token : error

简述问题
调用接口返回的内容字符串为拼接,无法正常转化为json格式:

"{\"ReturnCode\":1,\"Message\":\"\"}"

问题:

  • 字符床两边均多出双引号 ",应该为大括号 {}
  • 键值对中,参数名以及值采用双引号,所以出现很多转义斜杠 \

处理

  • 去外层引号

采用hutool工具,比较方便,也可定义两边不同符号。具体功能自行发掘

String string = StrUtil.strip(httpResponse.body(), "\"");
  • 转移字符串

采用lang3下StringEscapeUtils.unescapeJava();方法,但是已被标为已过期,根据提示引入commons-text包,并调用此包下的此方法即可。

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.9</version>
</dependency>
string = StringEscapeUtils.unescapeJava(string);

结果

JSONObject jSONObject1 = JSONObject.parseObject(string);
相关文章
|
7月前
|
小程序 API 开发者
【异常解决】“errcode“:47003,“errmsg“:“argument invalid! data.date4.value invalid rid:xxxxxx
【异常解决】“errcode“:47003,“errmsg“:“argument invalid! data.date4.value invalid rid:xxxxxx
223 0
【异常解决】“errcode“:47003,“errmsg“:“argument invalid! data.date4.value invalid rid:xxxxxx
|
5月前
|
SQL Oracle 关系型数据库
【已解决】ORA-01722: invalid number
【已解决】ORA-01722: invalid number
46 0
|
6月前
|
XML Java 数据库连接
BindingException:Invalid bound statement not found
BindingException:Invalid bound statement not found
29 0
|
7月前
|
编译器
解决Invalid bound statement (not found)错误~
解决Invalid bound statement (not found)错误~
|
10月前
|
JSON Java 数据格式
JSONException: illegal identifier : \pos 1 异常报错问题
JSONException: illegal identifier : \pos 1 异常报错问题
457 0
|
11月前
error C2041: illegal digit ‘9‘ for base ‘8‘ | error C2059: syntax error: ‘bad suffix on number‘
error C2041: illegal digit ‘9‘ for base ‘8‘ | error C2059: syntax error: ‘bad suffix on number‘
95 0
使用 spritesmith 报错 Error: Invalid file signature
使用 spritesmith 报错 Error: Invalid file signature
86 0
使用 spritesmith 报错 Error: Invalid file signature
|
SQL Java 数据库连接
Invalid bound statement (not found) 问题处理
您好,我是码农飞哥,感谢您阅读本文!本文将介绍使用MyBatis时出现的一些常见问题。
204 0
error: invalid argument ‘-std=gnu++98‘ not allowed with ‘C‘
error: invalid argument ‘-std=gnu++98‘ not allowed with ‘C‘
90 0
|
XML SQL 数据格式
Invalid bound statement (not found)错误的可能原因
Invalid bound statement (not found)错误的可能原因
424 0