【问题标题】:JsonArray parsingJsonArray 解析
【发布时间】:2012-08-22 21:00:14
【问题描述】:

我正在解析一个 json 文件,但收到以下消息:org.json.JSONException: End of input at character 0 of 文件内容为:

【问题讨论】:

  • 贴出相关的json解析代码,我们会帮助你更多
  • End of input at character 0 of 什么?你错过了告诉我们问题出在哪里的错误部分。我希望它会说“第 ____ 行”之类的内容。

标签: android json getjson


【解决方案1】:

您确定文件末尾没有空行吗?像这样:

[
{
code: "UNLC",
cours_jour: "40 020",
variation: "0.00"
},
{
code: "UNXC",
cours_jour: "7 450",
variation: "0.00"
}
]
<-- Empty line here!

【讨论】:

    【解决方案2】:

    您的 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 服务存在错误

    http://drupal.org/node/1433436

    【讨论】:

    • json 文件是由 drupal 模块生成的。我真的不知道模块的名称,但我使用他们给我的 json 文件..
    • 听起来模块创建的数组对象没有在它的键周围加上引号..IE $a = array("code" => "$code", "cours_jour" => "$ cours_jour", "variation" => "$variation");
      json_encode($a);但他们正在做 $a = array(code => "$code", cours_jour => "$cours_jour", variant => "$variation");
    • 嗨。你真的得到了代码,cours_jours 和变化。??我做 System.out.println(response) int try catch 但我什么都没有
    • 您是否阅读过链接drupal.org/node/1433436 中的错误信息——尝试按照链接中的说明切换模式,然后尝试我的代码
    猜你喜欢
    • 1970-01-01
    • 2014-06-04
    • 2016-08-12
    • 1970-01-01
    • 2017-08-13
    • 2016-08-17
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多