-1

I have the output in following JSONObject format. I want to fetch each and every value in this JSONObject. How can I do it? Need help! Thank you in advance.

{
      "410": 19082,
      "443": 19097,
      "667": 19172
}
Juan Cruz Soler
  • 8,172
  • 5
  • 41
  • 44
  • What did you try so far? – Prerak Sola Aug 01 '16 at 15:29
  • try this http://stackoverflow.com/a/7304091/2562861 – Shijil Aug 01 '16 at 15:30
  • I tried using Iterator class and then tried to fetch the value using keys.next(). But no luck. :( – Kishan Pawar Aug 01 '16 at 15:30
  • Post the code that you tried and the issue you have with that. – Prerak Sola Aug 01 '16 at 15:31
  • @PrerakSola I am trying something like this `JSONObject newjsonobject1 = null; for(int i = 0;i keys = newjsonobject1.keys(); while(keys.hasNext()) { String current = (String)keys.next(); String value = newjsonobject1.getString(current); Log.e("Fetched value________",value); }` – Kishan Pawar Aug 01 '16 at 15:45
  • @Shijil I saw that post and it is the same as I am trying to implement, but not working. – Kishan Pawar Aug 01 '16 at 15:49
  • Got the solution guys, the code I posted is working now. It was an error on my server side scripting. I just changed the signature on server side of JSONObject. Thank You! Above posted is the code to fetch the random distinct values in the JSONObject. – Kishan Pawar Aug 01 '16 at 15:55

1 Answers1

0

you can used below login for parsing key's value's of JSON

 String json ="{\n" +
            "      \"410\": 19082,\n" +
            "      \"443\": 19097,\n" +
            "      \"667\": 19172\n" +
            " }";
    try {
        JSONObject obj = new JSONObject(json);
        for(int i = 0; i<obj.length(); i++){
            Log.e("json parse", "Key = " + obj.names().getString(i) + " value = " + obj.get(obj.names().getString(i)));
        }

    }catch (Exception e)
    {
        e.printStackTrace();
    }
Damodhar
  • 1,219
  • 8
  • 17