0

I am following this tutorial to parse JSON Data

and now i know how to parse simple JSON with single JSON Array, but now i have to parse multilevel JSON

like i am able to parse 1st Level list but don't know how to parse second level, see my JSON Format below:

{
            "batches": [
                {
                    "title": "Beginners",
                    "students": [
                        {
                            "name": "Jack"
                        },
                        {
                            "name": "Lam"
                        },
                        {
                            "name": "Temp"
                        }
                    ]
                },
                {
                    "title": "Intermediate",
                    "students": [
                        {
                            "name": "Ashta"
                        }
                    ]
                },
                {
                    "title": "Advance",
                    "students": [
                        {
                            "name": "Kamak"
                        },
            {
                            "name": "Basi"
                        }
                    ]
                }
            ]
        }

My first level JSON Parsing result like this:

Beginners

Intermediate

Advance

now i want whenever user do tap on Beginners need to list their records:

Jack

Lam

Temp

MainActivity.java:-

public class MainActivity extends ActionBarActivity {
    ProgressDialog progressDialog;
    JSONObject jsonObject;
    JSONArray jsonArrayBatches, jsonArrayStudents;
    ArrayList<HashMap<String, String>> arrayList;
    ListView listView;
    ListViewAdapter listViewAdapter;
    static String NODE_BATCH_TITLE = "title";
    static String NODE_STUDENT_NAME = "name";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_main);
        // execute AsyncTask
        new JSONData().execute();
    }
    
    private class JSONData extends AsyncTask<Void, Void, Void>
    {

        protected void onPreExecute()
        {
            super.onPreExecute();           
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setIcon(R.drawable.ic_launcher);
            progressDialog.setTitle("JSON Parsing");
            progressDialog.setMessage("Loading...");
            progressDialog.show();
            Log.d("onPreExecute()::", "onPreExecute()");
        }
        
        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            Log.d("doInBackground()::", "doInBackground()");
            // initializing ArrayList
            arrayList = new ArrayList<HashMap<String, String>>();
            
            jsonObject = JSONParsing.getJSONfromURL("URL");
            try {
                
                // locate the array name in JSON
                jsonArrayBatches = jsonObject.getJSONArray("batches");
                // using for loop to get the length of json array
                for (int b = 0; b < jsonArrayBatches.length(); b++) {
                    // get the each and every json object from array
                    jsonObject = jsonArrayBatches.getJSONObject(b);
                    // name,value pair
                    HashMap<String, String> hashMapBatches = new HashMap<String, String>();
                    // put data into HashMap & convert json object to string
                    hashMapBatches.put("title", jsonObject.getString("title")); // node name = name (in JSON)                   
                    // add HashMap to ArrayList
                    arrayList.add(hashMapBatches); 
                }
                // locate the array name in JSON
                jsonArrayStudents = jsonObject.getJSONArray("students");
                // using for loop to get the length of json array
                for (int b = 0; b < jsonArrayBatches.length(); b++) {
                    // get the each and every json object from array
                    jsonObject = jsonArrayBatches.getJSONObject(b);
                    // name,value pair
                    HashMap<String, String> hashMapBatches = new HashMap<String, String>();
                    // put data into HashMap & convert json object to string
                    hashMapBatches.put("name", jsonObject.getString("name")); // node name = name (in JSON)                 
                }
            
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
        
        protected void onPostExecute(Void args)
        {
            // reference to ListView
            listView = (ListView) findViewById(R.id.listview);
            // put ArrayList to Adapter
            listViewAdapter = new ListViewAdapter(MainActivity.this, arrayList);
            // set Adapter to ListView
            listView.setAdapter(listViewAdapter);
            progressDialog.dismiss();
            Log.d("onPostExecute()::", "onPostExecute()");
            
            listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {
                    // TODO Auto-generated method stub
                    Toast.makeText(MainActivity.this, arrayList.get(position).get(MainActivity.NODE_STUDENT_NAME), Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    startActivity(intent);
                }
            });
        }
        
    }
}

SecondActivity.java:-

public class SecondActivity extends ActionBarActivity {
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_main);
    }

}
3
  • 2
    Use GSON that will be far more easy and faster than developing this sort of logic.. Commented Jun 20, 2014 at 11:04
  • here it the link code.google.com/p/google-gson/downloads/list Commented Jun 20, 2014 at 11:06
  • After first level, you have to make one loop of "batches" json array size, inside that you need to do same process again. Commented Jun 20, 2014 at 11:08

3 Answers 3

1

Hello You Can check here and follow the solution and proper way to handling JSON..

1.MainActivity

2.SecondActivity

3.FirstListAdapter

4.SecondListAdapter

You can understand full JSON Parsing here..

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

1 Comment

such a easy way ! i think the best way, we can get this work to done thank you so much :)
1

Try This...

//first level 
jsonArrayBatches = jsonObject.getJSONArray("batches");
        // using for loop to get the length of json array
        for (int b = 0; b < jsonArrayBatches.length(); b++) {
            // get the each and every json object from array
            jsonObject = jsonArrayBatches.getJSONObject(b);
            // name,value pair
            HashMap<String, String> hashMapBatches = new HashMap<String, String>();
            // put data into HashMap & convert json object to string
            // hashMapBatches.put("title", jsonObject.getString("title"));

                    //Second level 
                JSONArray json_second = new JSONArray("students");
                for (int i=0;i<json_second.length();i++)
                {
                     JSONObject jsonObject = json_second.getJSONObject(i);
                     String name = jsonObject.getString("name");
                             hashMapBatches.put("name", name);
                }
           }

8 Comments

By Adding this code to your current code you could get second level Array which is "students"
yes i got it, but i have written code in MainActivity.java and have to parse tapped list item using Intent, and don't know what to write in SecondActivity.java !!
she must have to search it...its not gonna work here
@DevCarlsberg i want a way to get my work done, if you will see above i have written whole code and i don't have time to waste
@Prag's actually i am working on solution which you provided me and will let you know OK
|
0

Don't use ready made answer first you have to understand JSON using this link.

JSON LINK. Because you have to understand exactly what you want to do with JSON. and

to verify your JSON and Identify which is JSON String, JSON Array, JSON Object i suggest you to use

JSON Formate, here you have to pass your JSON data or use live URL and check which one is Array, Object and String.

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.