Skip to content Skip to sidebar Skip to footer

How To Manage And Read Json Array From Php In Android

I've lots of json array which are formatted in this way: [ { 'cod_ent':'953', 'name_ent':'example1', 'amb':{ '15':{ 'cod_amb':'002',

Solution 1:

You can Iterate JSONObject Like this:

JSONObject jsonObject =getfromarray.getJSONObject("amb")
    Iterator<String> iter = jsonObject.keys();
    while (iter.hasNext()) {
        String key = iter.next();
        Log.w("Key", key);
        try {
            JSONObject js = jsonObject.getJSONObject(key);

            Log.w("cod_amb", js.getString("cod_amb"));
            Log.w("name_amb", js.getString("name_amb"));
        } catch (Exception e) {
            // TODO: handle exception
        }

    }

Solution 2:

you can retrieve the content of amb with

JSONObject amb = getfromarray.optJSONObject("amb"); 

and you don't know the keys in amb, you can use keys(), to retrieve an Iterator<String>, which contains all the keys of amb

Iterator<String> ambKeysIterator = amb.keys();
   while(ambKeysIterator.hasNext()) {
       String key = ambKeysIterator.next();
       JSONObject obj = amb.optJSONObject(key);
       if (obj != null) {
             // parse obj
       }
   }

Solution 3:

amb is another JSONObject. You should access it like so:

JSONObject amb = jsonArray.getJSONObject("amb");
JSONObject fifteen = jsonArray.getJSONObject("15");
String cod_amb = fifteen.getString("cod_amb");

Solution 4:

You can get a JSONObject from another JSONObject. Your code can be like this-

JSONObject amb =getfromarray.getJSONObject("amb")

EDIT:-

Perhaps you are looking for this

Solution 5:

"amb" is an array. So you should do something like

getfromarray.getJSONArray("amb");

in order to get a new JsonArray that you will parse again... and so on

Post a Comment for "How To Manage And Read Json Array From Php In Android"