【发布时间】:2017-12-14 17:35:27
【问题描述】:
我在解析 JSON 时遇到问题。我总是尝试这样做,返回结果如下:位置0处的意外字符()。
public Object execute(HttpRequestBase request){
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = null;
Object object = null;
try {
response = client.execute(request);
InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader((is)));
StringBuilder builder = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
builder.append(output).append("\n");
}
if (response.getStatusLine().getStatusCode() == 200) {
object = new JSONParser().parse(builder.toString());
client.getConnectionManager().shutdown();
} else {
LOG.log(Level.SEVERE, builder.toString());
throw new RuntimeException(builder.toString());
}
} catch (IOException | ParseException ex) {
LOG.log(Level.SEVERE, ex.toString());
} finally {
}
return object;
}
PS:
- 我的响应返回格式正确的 JSON;
- 运行这段代码时会出现问题
object = new JSONParser().parse(builder.toString());
这是我的 JSON 文件的一部分:
[
{
"id":2115,
"identificacao":"17\/2454634-6",
"ultima_atualizacao":null
},
{
"id":2251,
"identificacao":"17\/3052383-2",
"ultima_atualizacao":"2017-11-21"
},
{
"id":2258,
"identificacao":"17\/3070024-6",
"ultima_atualizacao":null
},
{
"id":2257,
"identificacao":"17\/3070453-5",
"ultima_atualizacao":null
}
]
【问题讨论】:
-
你能在解析之前显示
builder的内容吗?我不明白您为什么要添加换行符,但从调试的角度来看会有所帮助。 -
JSON 在哪里?
-
检查 JSON 输入,它可能以 UTF-8 BOM 开头,您需要在解析之前将其剥离。
-
@Eric 我确实将 JSON 放在了帖子的最后。
标签: java json jsonparser