问题描述
我正在尝试从 URL 检索 JSON 数据,但收到以下错误:
非法字符((CTRL-CHAR,代码 31)):令牌之间只允许使用常规空格(
、
、 )
我的代码:
final URI uri = new URIBuilder(UrlConstants.SEARCH_URL).addParameter("关键字", searchTerm).addParameter("计数", "50").建造();node = new ObjectMapper().readTree(new URL(uri.toString()));<<<<<抛出错误
构造的url为即https://www.example.org/api/search.json?keywords=iphone&count=50
这里出了什么问题?以及如何成功解析这些数据?
进口:
import com.google.appengine.repackaged.org.codehaus.jackson.JsonNode;导入 com.google.appengine.repackaged.org.codehaus.jackson.map.ObjectMapper;导入 com.google.appengine.repackaged.org.codehaus.jackson.node.ArrayNode;导入 org.apache.http.client.utils.URIBuilder;
示例响应
{元:{索引允许:假},产品: {产品: [{编号:1,名称:Apple iPhone 6 16GB 4G LTE GSM 工厂解锁"},{编号:2,名称:Apple iPhone 7 8GB 4G LTE GSM 工厂解锁"}]}}
该消息应该是不言自明的:
您正在处理的 JSON 中存在非法字符(在本例中为字符代码 31,即控制代码单元分隔符").
换句话说,您收到的数据不是正确的 JSON.
背景:
JSON 规范(RFC 7159)说:
- 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:
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 :-).
这篇关于杰克逊错误“非法字符......只允许使用常规空格"解析 JSON 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!