Android org.json.jsonarray无法转换为jsonobject错误

时间:2016-10-04 22:00:34

标签: java android json api

我是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"
        }
      ]
    }
  }
}

1 个答案:

答案 0 :(得分:0)

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

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