且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

杰克逊错误“非法字符......只允许使用常规空格"解析 JSON 时

更新时间:2022-10-17 21:31:46

该消息应该是不言自明的:

您正在处理的 JSON 中存在非法字符(在本例中为字符代码 31,即控制代码单元分隔符").

换句话说,您收到的数据不是正确的 JSON.


背景:

JSON 规范(RFC 7159)说:

  1. JSON 语法

JSON 文本是一个令牌序列.令牌集包括六个结构字符、字符串、数字和三个字面名称.

[...]

在任何之前或之后都允许有无关紧要的空格六个结构字符.

ws = *(

%x20/;空间

%x09/;水平标签

%x0A/;换行或换行

%x0D ) ;回车

换句话说:JSON 可能包含令牌之间的空白(令牌"表示 JSON 的一部分,即列表、字符串等),但空白"不包含在这些令牌之间.定义为仅表示空格、制表符、换行符和回车符.

您的文档包含其他内容(代码 31),其中只允许使用空格,因此不是有效的 JSON.


解析:

不幸的是,您使用的 Jackson 库没有提供解析这种格式错误数据的方法.要成功解析它,您必须在 Jackson 处理 JSON 之前对其进行过滤.

您可能必须自己从 REST 服务中检索(伪)JSON,使用标准 HTTP,例如java.net.HttpUrlConnection.然后适当地过滤掉坏的"字符,并将结果字符串传递给 Jackson.如何做到这一点完全取决于您如何使用 Jackson.

如果您遇到问题,请随时提出单独的问题:-).

I am trying to retrieve JSON data from a URL but get the following error:

Illegal character ((CTRL-CHAR, code 31)):
only regular white space (
, 
,	) is allowed between tokens

My code:

final URI uri = new URIBuilder(UrlConstants.SEARCH_URL)
      .addParameter("keywords", searchTerm)
      .addParameter("count", "50")
      .build();
  node = new ObjectMapper().readTree(new URL(uri.toString())); <<<<< THROWS THE ERROR

The url constructed is i.e https://www.example.org/api/search.json?keywords=iphone&count=50

What is going wrong here? And how can I parse this data successfully?


Imports:

import com.google.appengine.repackaged.org.codehaus.jackson.JsonNode;
import com.google.appengine.repackaged.org.codehaus.jackson.map.ObjectMapper;
import com.google.appengine.repackaged.org.codehaus.jackson.node.ArrayNode;
import org.apache.http.client.utils.URIBuilder;

example response

{
    meta: {
        indexAllowed: false
    },
    products: {
        products: [ 
            {
                id: 1,
                name: "Apple iPhone 6 16GB 4G LTE GSM Factory Unlocked"
            },
            {
                id: 2,
                name: "Apple iPhone 7 8GB 4G LTE GSM Factory Unlocked"
            }
        ]
    }
}

The message should be pretty self-explanatory:

There is an illegal character (in this case character code 31, i.e. the control code "Unit Separator") in the JSON you are processing.

In other words, the data you are receiving is not proper JSON.


Background:

The JSON spec (RFC 7159) says:

  1. JSON Grammar

A JSON text is a sequence of tokens. The set of tokens includes six tructural characters, strings, numbers, and three literal names.

[...]

Insignificant whitespace is allowed before or after any of the six structural characters.

ws = *(

%x20 / ; Space

%x09 / ; Horizontal tab

%x0A / ; Line feed or New line

%x0D ) ; Carriage return

In other words: JSON may contain whitespace between the tokens ("tokens" meaning the part of the JSON, i.e. lists, strings etc.), but "whitespace" is defined to only mean the characters Space, Tab, Line feed and Carriage return.

Your document contains something else (code 31) where only whitespace is allowed, hence is not valid JSON.


To parse this:

Unfortunately, the Jackson library you are using does not offer a way to parse this malformed data. To parse this successfully, you will have to filter the JSON before it is handled by Jackson.

You will probably have to retrieve the (pseudo-)JSON yourself from the REST service, using standard HTTP using, e.g. java.net.HttpUrlConnection. Then suitably filter out "bad" characters, and pass the resulting string to Jackson. How to do this exactly depends on how you use Jackson.

Feel free to ask a separate questions if you are having trouble :-).