Peppegiuseppe
Peppegiuseppe

Reputation: 183

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",
            "name_amb":"Or11"
         },
         "1723":{
            "cod_amb":"00009",
            "name_amb":"P3o1"
         }
      }
   }
]

and i'd like to read it correctly in android. I try with this code and i manage to retrieve the first two entries ("cod_ent" and "name_ent") but i'm still not able to manage "amb" sub-array.

        JSONObject json = null;
        try {
            InputStream pre_json = response.getEntity().getContent();
            json = new JSONObject("{data:"+convertStreamToString(pre_json)+"}");
        } catch (IOException e) {
            e.printStackTrace();
        }

        JSONArray jsonArray = json.getJSONArray("data");
        for (int i = 0; i < jsonArray.length(); i++){
            JSONObject getfromarray = jsonArray.getJSONObject(i);
            cod_ent = getfromarray.getString("cod_ent");
            name_ent = getfromarray.getString("name_ent");

            //how to get amb???


        }

Upvotes: 0

Views: 106

Answers (5)

Sanjeet A
Sanjeet A

Reputation: 5227

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

Upvotes: 0

Ankii Rawat
Ankii Rawat

Reputation: 2080

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
        }

    }

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

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
       }
   }

Upvotes: 1

user3793589
user3793589

Reputation: 418

"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

Upvotes: -1

roarster
roarster

Reputation: 4086

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");

Upvotes: 0

Related Questions