报错:JSONException: illegal identifier : \pos 1, line 1, column 2 或not close json

629 阅读1分钟
  • 调用接口返回的内容字符串为拼接,无法正常转化为json格式:

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

  • 问题:

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

    • 采用hutool工具,比较方便,也可定义两边不同符号。具体功能自行研究
    String string = StrUtil.strip(httpResponse.body(), "\"");
    
    • 采用langStringEscapeUtils.unescapeJava();
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-text</artifactId>
        <version>1.9</version>
    </dependency>
    
    string = StringEscapeUtils.unescapeJava(string);
    

    结果:

    JSONObject jSONObject = JSONObject.parseObject(string);