杰克逊错误“非法字符......只允许常规空格”解析JSON时 [英] Jackson error "Illegal character... only regular white space allowed" when parsing JSON

查看:5911
本文介绍了杰克逊错误“非法字符......只允许常规空格”解析JSON时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从网址中检索JSON数据,但会收到以下错误:

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 (\r, \n,\t) is allowed between tokens

我的代码:

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

构建的网址是 https://www.example.org/api/search.json?keywords=iphone&count=50

这里出了什么问题?如何成功解析这些数据?

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

进口:

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;

示例回复

{
    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:

有一个非法字符(在这种情况下是字符代码) 31,即您正在处理的JSON中的控制代码Unit Separator。

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

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

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

背景:

JSON规范( RFC 7159 )说:

The JSON spec (RFC 7159) says:



  1. JSON Grammar

JSON文本是一系列标记。这组令牌包括六个
结构字符,字符串,数字和三个文字名称。

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 /;空格

%x09 /;水平标签

%x09 / ; Horizontal tab

%x0A /;换行或新行

%x0A / ; Line feed or New line

%x0D);回车

换句话说:JSON可能包含令牌之间的空格(令牌表示JSON的一部分,即列表,字符串等),但whitespace被定义为仅表示字符Space,Tab,Line feed和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.

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

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

解析这个:

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

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.

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

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 :-).

这篇关于杰克逊错误“非法字符......只允许常规空格”解析JSON时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆