0

My json response:

    {
    "Countries": [

        {

                "Country": "China",
                "Alpha2": "CN",
                "Alpha3": "CHN",

                "numbers": {
                    "Jon": "100",
                    "Mat": "200",
                    "SAM": "300"


                }
        },
         {

                "Country": "Italy",
                "Alpha2": "IT",
                "Alpha3": "ITA",

                "numbers": {
                    "Benny": "400",
                    "Jack": "500"


                }
         }

    ]
}

My code:

for (int i = 0; i < m_jArry.length(); i++) {
            JSONObject jo_inside = m_jArry.getJSONObject(i);

            if (jo_inside.getString("Country").equals("Italy"))
            {
                Log.d("DTAG",jo_inside.getString("Country"));
                Log.d("DTAG",jo_inside.getString("Alpha2"));
                Log.d("DTAG", jo_inside.getString("Alpha3"));

                JSONObject jo_inside2 = jo_inside.getJSONObject("numbers");
                Log.d("DTAG", "Size: "+jo_inside2.length());

                for (int j=0;j<jo_inside2.length();j++)
                {
                    //???
                }
            }

How can I get the name of value and his value? For this example I need to get Benny 400, Jack 500

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Dim
  • 4,527
  • 15
  • 80
  • 139
  • 2
    I think it is what you are searching for : http://stackoverflow.com/questions/13573913/android-jsonobject-how-can-i-loop-through-a-flat-json-object-to-get-each-key-a – Blunderer Jan 05 '16 at 11:41
  • This is more complex post but it does answer your question http://stackoverflow.com/questions/12870332/how-can-i-iterate-jsonobject-to-get-individual-items – Rohit5k2 Jan 05 '16 at 11:42

4 Answers4

2

you can get all keys by using json.keys, in your case try with following code.

  Iterator<String> iter = jo_inside2.keys();
  while (iter.hasNext()) {
    String key = iter.next();
    try {
        Object value = jo_inside2.get(key);
    } catch (JSONException e) {
        //exception
    }
  }
Ravi
  • 34,851
  • 21
  • 122
  • 183
2
 if (jo_inside.getString("Country").equals("Italy"))
            {
                Log.d("DTAG",jo_inside.getString("Country"));
                Log.d("DTAG",jo_inside.getString("Alpha2"));
                Log.d("DTAG", jo_inside.getString("Alpha3"));

                JSONObject jo_inside2 = jo_inside.getJSONObject("numbers");
                Log.d("DTAG", "Size: "+jo_inside2.length());

                for (int j=0;j<jo_inside2.length();j++)
                {
                    Iterator<?> keys = jo_inside2.keys();

                while( keys.hasNext() ) 
                  {
                   String key = (String)keys.next();
                    if ( jo_inside2.get(key) instanceof JSONObject ) 
                     {
                           Object value = jo_inside2.get(key);
                     }
                   }
                  }
            }
rahul
  • 1,095
  • 8
  • 22
1

// get all keys from JSONObj

    Iterator<String> iterator = jo_inside2.keys();
      while (iterator.hasNext()) {
        String key = iterator.next();
        Log.i("TAG","key:"+key +"--Value::"+categoryJSONObj.optString(key);

  }
CodingRat
  • 1,934
  • 3
  • 23
  • 43
0
    for(Iterator iterator =  jo_inside2.keys(); iterator.hasNext();) {
       String key = (String) iterator.next();
       System.out.println(key+" - "+jo_inside2.get(key));

      //Log.i("TAG",key +" - "+jo_inside2.get(key));
     }
Rohit
  • 538
  • 5
  • 10