【问题标题】:Android org.json.jsonarray cannot be converted to jsonobject errorAndroid org.json.jsonarray 无法转换为 jsonobject 错误
【发布时间】:2016-10-04 22:00:33
【问题描述】:

我是 JSON 新手,但我尝试使用所有答案但没有奏效。请帮忙,我做错了什么。

    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        progressDialog.dismiss();
        editText.setText(s);
        try {
            JSONObject jsonObject = new JSONObject(s);
            JSONArray resultArray = jsonObject.getJSONArray("query");

            addresses = new ArrayList<String>();
            for (int i = 0; i<resultArray.length(); i++){
               addresses.add(resultArray.getJSONObject(i).getString("Name"));
                Log.d("DTA",resultArray.getJSONObject(i).getString("Name"));

            }
            refreshAdapter();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

}

这是我用于解析的 JSON。

{  
  "query":{  
    "count":2,
    "created":"2016-10-04T19:50:08Z",
    "lang":"ru",
    "results":{  
      "rate":[  
        {  
          "id":"USDRUB",
          "Name":"USD/RUB",
          "Rate":"62.8240",
          "Date":"10/4/2016",
          "Time":"7:21pm",
          "Ask":"62.8416",
          "Bid":"62.8240"
        },
        {  
          "id":"EURRUB",
          "Name":"EUR/RUB",
          "Rate":"70.3460",
          "Date":"10/4/2016",
          "Time":"7:21pm",
          "Ask":"70.3740",
          "Bid":"70.3460"
        }
      ]
    }
  }
}

【问题讨论】:

  • 你能发布更多关于错误的细节吗?一些日志,也许。
  • 10-05 07:30:01.604 11310-11310/com.harbinger.currency W/System.err: org.json.JSONException: Value {"count":2,"created":" 2016-10-05T04:30:00Z","lang":"en-US","re​​sults":{"rate":[{"id":"USDRUB","Name":"USD\/RUB" ,"价格":"62.6486","日期":"10\/5\/2016","时间":"0:57am","要价":"62.7480","出价":"62.6486"}, {"id":"EURRUB","Name":"EUR\/RUB","Rate":"70.2230","Date":"10\/5\/2016","Time":"0:57am org.json.JSONObject 类型的查询中的 ","Ask":"70.3570","Bid":"70.2230"}]}} 无法转换为 JSONArray

标签: java android json api


【解决方案1】:

那是因为query 不是一个数组,它是一个对象。我猜你想从rate 获取对象,这是怎么做的:

 try {
        JSONObject jsonObject = new JSONObject(s);
        JSONObject resultsObject = jsonObject.getJSONObject("results");
        JSONArray resultArray = resultsObject.getJSONArray("rate");
        ... etc... now iterate

【讨论】:

  • 10-05 07:42:54.532 15781-15781/com.harbinger.currency W/System.err: org.json.JSONException: 结果没有价值
  • 尝试 { JSONObject jsonObject = new JSONObject(s); JSONObject resultsObject = jsonObject.getJSONObject("results"); JSONArray resultArray = resultsObject.getJSONArray("rate");地址 = 新的 ArrayList(); for (int i = 0; i
  • 它不工作。 Studio 看不到任何结果值 :(
猜你喜欢
  • 2014-02-03
  • 1970-01-01
  • 2013-06-30
  • 2018-05-12
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多