我正在解析一个 json 文件,但收到以下消息:org.json.JSONException: End of input at character 0 of character 0 of the file of the file is:
问问题
261 次
2 回答
1
您确定文件末尾没有空行吗?像这样:
[
{
code: "UNLC",
cours_jour: "40 020",
variation: "0.00"
},
{
code: "UNXC",
cours_jour: "7 450",
variation: "0.00"
}
]
<-- Empty line here!
于 2012-08-17T17:45:01.537 回答
0
您的 JSON 对象字段需要用引号封装
IE
{
"code": "BOAC",
"cours_jour": "29 000",
"variation": "-1.69"
}
JSON 文件是如何生成的?
- 编辑
您可以使用以下代码将页面下载为字符串,然后将其转换为 JSONArray,然后拉取每个 JSONObject。您不能在主线程上运行任何 Web 请求,因此要么扩展新的异步任务或线程,要么可以运行以执行以下操作
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("http://www.impaxis-securities.com/securities/cours-actions/cours.json");
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response;
try {
response = httpclient.execute(httpost, responseHandler);
JSONArray arr = new JSONArray(response);
int arrLength = arr.length();
if(arrLength > 0)
{
for(int i = 0; i < arrLength; i++)
{
JSONObject item = arr.getJSONObject(i);
String code = item.getString("code");
String cours_jour = item.getString("cours_jour");
String variation = item.getString("variation");
//Either insert to a DB or add to an array list and return it
}
}
}
catch (ClientProtocolException e) {
//Issue with web server
}
catch (IOException e) {
//Issue with request
}
catch (JSONException e) {
//ISSUE Parsing JSON from site
}
- -编辑
我测试了代码,看起来 JSON 插件/REST 服务存在错误
于 2012-08-17T17:49:45.660 回答