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"
        }
      ]
    }
  }
}
4
  • see stackoverflow.com/questions/17441246/… Commented Oct 4, 2016 at 22:05
  • Can you post some more detail on the error? Some logs, maybe. Commented Oct 4, 2016 at 22:08
  • Possible duplicate of How to parse JSON in Android Commented Oct 4, 2016 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 Commented Oct 5, 2016 at 4:31

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

10-05 07:42:54.532 15781-15781/com.harbinger.currency W/System.err: org.json.JSONException: No value for results
try { JSONObject jsonObject = new JSONObject(s); JSONObject resultsObject = jsonObject.getJSONObject("results"); JSONArray resultArray = resultsObject.getJSONArray("rate"); 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")); }
It's not working. Studio don't see any values for results :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.