0

I'm new in JSON, but I try to use all answers and didn't work. Help please, what I doing wrong.

    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();
        }
    }
}

}

It's my JSON for parsing.

{  
  "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"
        }
      ]
    }
  }
}
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
  • see https://stackoverflow.com/questions/17441246/org-json-jsonarray-cannot-be-converted-to-jsonobject?rq=1 – njzk2 Oct 04 '16 at 22:05
  • Can you post some more detail on the error? Some logs, maybe. – stefanobaghino Oct 04 '16 at 22:08
  • Possible duplicate of [How to parse JSON in Android](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) – Nongthonbam Tonthoi Oct 04 '16 at 22:23
  • 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","results":{"rate":[{"id":"USDRUB","Name":"USD\/RUB","Rate":"62.6486","Date":"10\/5\/2016","Time":"0:57am","Ask":"62.7480","Bid":"62.6486"},{"id":"EURRUB","Name":"EUR\/RUB","Rate":"70.2230","Date":"10\/5\/2016","Time":"0:57am","Ask":"70.3570","Bid":"70.2230"}]}} at query of type org.json.JSONObject cannot be converted to JSONArray – Oleg Gerstev Oct 05 '16 at 04:31

1 Answers1

0

That's because query is not an array, it is an object. I guess you want to get the objects from rate, this is how to do it:

 try {
        JSONObject jsonObject = new JSONObject(s);
        JSONObject resultsObject = jsonObject.getJSONObject("results");
        JSONArray resultArray = resultsObject.getJSONArray("rate");
        ... etc... now iterate
slanecek
  • 808
  • 1
  • 8
  • 24
  • 10-05 07:42:54.532 15781-15781/com.harbinger.currency W/System.err: org.json.JSONException: No value for results – Oleg Gerstev Oct 05 '16 at 04:43
  • try { JSONObject jsonObject = new JSONObject(s); JSONObject resultsObject = jsonObject.getJSONObject("results"); JSONArray resultArray = resultsObject.getJSONArray("rate"); addresses = new ArrayList(); for (int i = 0; i – Oleg Gerstev Oct 05 '16 at 04:48
  • It's not working. Studio don't see any values for results :( – Oleg Gerstev Oct 05 '16 at 04:51