0

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


        }
0

5 Answers 5

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
        }

    }
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

1 Comment

I don't know all the label "15", "1723" .. etc etc.. how can i use your code in a for loop?
0

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

2 Comments

ok.. and how can i get all the array in this new json object? in the same way with a for loop?
As I see in the JSON you posted amb is not an array it is having JSONObjects only.
-1

"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

Comments

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.