简体   繁体   中英

How to parse JSON into ListView

I am trying to parse JSON data into List, I want to achieve this:-

Category List -> Sub-Category List

My JSON look like this:

[
    {
         "categoryName" : "Killer",
         "categoryList" : [{"ProductID": "1",
                            "ProductName": "Jeans"},
                           {"ProductID": "2",
                            "ProductName": "Tees"}]
   },

   {
         "categoryName" : "Blackberry",
         "categoryList" : [{"ProductID": "1",
                            "ProductName": "Trousers"},
                           {"ProductID": "2",
                            "ProductName": "Shirts"}]
   }    
]

By using below code i am able to fetch Category List, CategoryActivity.java :

  public class CategoryActivity extends ListActivity {
// Connection detector
ConnectionDetector cd;

// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();

// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();

ArrayList<HashMap<String, String>> categoriesList;

// albums JSONArray
JSONArray categories = null;

// albums JSON url
static final String URL_CATEGORIES = "http://domain.it/keyurls/multi.json";

// ALL JSON node names
private static final String CATEGORY_NAME = "categoryName";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category);

    cd = new ConnectionDetector(getApplicationContext());

    // Check for internet connection
    if (!cd.isConnectingToInternet()) {
        // Internet Connection is not present
        alert.showAlertDialog(CategoryActivity.this, "Internet Connection Error",
                "Please connect to working Internet connection", false);
        // stop executing code by return
        return;
    }

    // Hashmap for ListView
    categoriesList = new ArrayList<HashMap<String, String>>();

    // Loading Albums JSON in Background Thread
    new LoadAlbums().execute();

    // get listview
    ListView lv = getListView();

    /**
     * Listview item click listener
     * TrackListActivity will be lauched by passing album id
     * */
    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                long arg3) {
            // on selecting a single album
            // TrackListActivity will be launched to show tracks inside the album
            Intent i = new Intent(getApplicationContext(), ProductActivity.class);

            // send album id to tracklist activity to get list of songs under that album
            String category_name = ((TextView) view.findViewById(R.id.category_name)).getText().toString();
            i.putExtra("category_name", category_name);             

            startActivity(i);
        }
    });     
}

/**
 * Background Async Task to Load all Albums by making http request
 * */
class LoadAlbums extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(CategoryActivity.this);
        pDialog.setMessage("Listing Categories ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting Categories JSON
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // getting JSON string from URL
        String json = jsonParser.makeHttpRequest(URL_CATEGORIES, "GET",
                params);

        // Check your log cat for JSON reponse
        Log.d("Categories JSON: ", "> " + json);

        try {               
            categories = new JSONArray(json);

            if (categories != null) {
                // looping through All albums
                for (int i = 0; i < categories.length(); i++) {
                    JSONObject c = categories.getJSONObject(i);

                    // Storing each json item values in variable
                    String name = c.getString(CATEGORY_NAME);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(CATEGORY_NAME, name);

                    // adding HashList to ArrayList
                    categoriesList.add(map);
                }
            }else{
                Log.d("Categories: ", "null");
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all albums
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        CategoryActivity.this, categoriesList,
                        R.layout.list_item_category, new String[] { CATEGORY_NAME }, new int[] {
                                R.id.category_name });

                // updating listview
                setListAdapter(adapter);
            }
        });
    }
}
 }

But whenever i do tap on any of the Category Item, getting blank activity in place of showing List of Products, see my code ProductActivity.java :

  public class ProductActivity extends ListActivity {
    // Connection detector
    ConnectionDetector cd;

    // Alert dialog manager
    AlertDialogManager alert = new AlertDialogManager();

     // Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jsonParser = new JSONParser();

    ArrayList<HashMap<String, String>> productsList;

    // products JSONArray
    JSONArray products = null;

    // Category name
    String category_name;

    private static final String CATEGORY_NAME = "categoryName";
    private static final String CATEGORY_LIST = "categoryList";
    private static final String PRODUCT_ID = "ProductID";
    private static final String PRODUCT_NAME = "ProductName";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product);

        cd = new ConnectionDetector(getApplicationContext());

        // Check if Internet present
        if (!cd.isConnectingToInternet()) {
            // Internet Connection is not present
            alert.showAlertDialog(ProductActivity.this, "Internet Connection Error",
                    "Please connect to working Internet connection", false);
            // stop executing code by return
            return;
        }

        // Get album id
        Intent i = getIntent();
        category_name = i.getStringExtra("category_name");

        // Hashmap for ListView
        productsList = new ArrayList<HashMap<String, String>>();

        // get listview
        ListView lv = getListView();

        /**
         * Listview on item click listener
         * SingleTrackActivity will be lauched by passing album id, song id
         * */
        lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                    long arg3) {

            }
        }); 
    }

     /**
     * Background Async Task to Load all tracks under one album
     * */
    class LoadTracks extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ProductActivity.this);
            pDialog.setMessage("Loading products ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

     /**
     * getting products json and parsing
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // post category name as GET parameter
        params.add(new BasicNameValuePair(CATEGORY_NAME, category_name));

        // getting JSON string from URL
        String json = jsonParser.makeHttpRequest(CategoryActivity.URL_CATEGORIES, "GET",
                params);

        // Check your log cat for JSON reponse
        Log.d("Product List JSON: ", json);

        try {
            JSONObject jObj = new JSONObject(json);
            if (jObj != null) {
                category_name = jObj.getString(CATEGORY_NAME);
                products = jObj.getJSONArray(CATEGORY_LIST);

                if (products != null) {
                    // looping through All songs
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

                        // Storing each json item in variable
                        String song_id = c.getString(PRODUCT_ID);
                        String name = c.getString(PRODUCT_NAME);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put("category_name", category_name);
                        map.put(PRODUCT_ID, song_id);
                        map.put(PRODUCT_NAME, name);                        

                        // adding HashList to ArrayList
                        productsList.add(map);
                    }
                } else {
                    Log.d("Categories: ", "null");
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

     /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all tracks
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */

                 ListAdapter adapter = new SimpleAdapter(
                         ProductActivity.this, productsList,
                         R.layout.list_item_product, new String[] { "category_name", PRODUCT_ID, PRODUCT_NAME}, 
                         new int[] {
                                 R.id.album_id, R.id.song_id, R.id.album_name });
                 // updating listview
                 setListAdapter(adapter);

                }
            });
        }
    }

So here my question is how to parse data for second level list and where i am doing wrong?

I am also unable to understand what Logcat says, see below:-

11-11 23:26:40.501: I/Choreographer(520): Skipped 54 frames!  The application may be doing too much work on its main thread.
11-11 23:26:41.681: D/Categories JSON:(921): > [
11-11 23:26:41.681: D/Categories JSON:(921):     {
11-11 23:26:41.681: D/Categories JSON:(921):          "categoryName" : "Killer",
11-11 23:26:41.681: D/Categories JSON:(921):          "categoryList" : [{"ProductID": "1",
11-11 23:26:41.681: D/Categories JSON:(921):                             "ProductName": "Jeans"},
11-11 23:26:41.681: D/Categories JSON:(921):                            {"ProductID": "2",
11-11 23:26:41.681: D/Categories JSON:(921):                             "ProductName": "Tees"}]
11-11 23:26:41.681: D/Categories JSON:(921):    },
11-11 23:26:41.681: D/Categories JSON:(921):    {
11-11 23:26:41.681: D/Categories JSON:(921):          "categoryName" : "Blackberry",
11-11 23:26:41.681: D/Categories JSON:(921):          "categoryList" : [{"ProductID": "1",
11-11 23:26:41.681: D/Categories JSON:(921):                             "ProductName": "Trousers"},
11-11 23:26:41.681: D/Categories JSON:(921):                            {"ProductID": "2",
11-11 23:26:41.681: D/Categories JSON:(921):                             "ProductName": "Shirts"}]
11-11 23:26:41.681: D/Categories JSON:(921):    }    
11-11 23:26:41.681: D/Categories JSON:(921): ]
11-11 23:26:42.281: I/Choreographer(921): Skipped 35 frames!  The application may be doing too much work on its main thread.
11-11 23:26:42.281: I/Choreographer(288): Skipped 30 frames!  The application may be doing too much work on its main thread.
11-11 23:27:40.051: I/ActivityManager(288): START u0 {cmp=com.example.androidhive/com.example.json.ProductActivity (has extras)} from pid 921
11-11 23:27:40.071: W/WindowManager(288): Screenshot failure taking screenshot for (164x291) to layer 21010
11-11 23:27:40.092: E/SoundPool(288): error loading /system/media/audio/ui/Effect_Tick.ogg
11-11 23:27:40.092: W/AudioService(288): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-11 23:27:40.092: E/SoundPool(288): error loading /system/media/audio/ui/Effect_Tick.ogg
11-11 23:27:40.102: W/AudioService(288): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-11 23:27:40.102: E/SoundPool(288): error loading /system/media/audio/ui/Effect_Tick.ogg
11-11 23:27:40.102: W/AudioService(288): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-11 23:27:40.102: E/SoundPool(288): error loading /system/media/audio/ui/Effect_Tick.ogg
11-11 23:27:40.102: W/AudioService(288): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-11 23:27:40.111: E/SoundPool(288): error loading /system/media/audio/ui/Effect_Tick.ogg
11-11 23:27:40.111: W/AudioService(288): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-11 23:27:40.111: E/SoundPool(288): error loading /system/media/audio/ui/KeypressStandard.ogg
11-11 23:27:40.122: W/AudioService(288): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
11-11 23:27:40.122: E/SoundPool(288): error loading /system/media/audio/ui/KeypressSpacebar.ogg
11-11 23:27:40.122: W/AudioService(288): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
11-11 23:27:40.122: E/SoundPool(288): error loading /system/media/audio/ui/KeypressDelete.ogg
11-11 23:27:40.122: W/AudioService(288): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
11-11 23:27:40.131: E/SoundPool(288): error loading /system/media/audio/ui/KeypressReturn.ogg
11-11 23:27:40.131: W/AudioService(288): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
11-11 23:27:40.131: W/AudioService(288): onLoadSoundEffects(), Error -1 while loading samples
11-11 23:27:40.462: V/PhoneStatusBar(393): setLightsOn(true)
11-11 23:27:40.741: I/ActivityManager(288): Displayed com.example.androidhive/com.example.json.ProductActivity: +585ms
11-11 23:39:00.151: D/dalvikvm(288): GC_FOR_ALLOC freed 637K, 61% free 5193K/13268K, paused 90ms, total 105ms
    // You need to add this in for loop of urs 

 JSONArray  list = new JSONArray    = c.getString(CATEGORY_NAME); 
 for (int j = 0; j < list.length(); j++) {
// again parse it 
}
public class AndroidJSONParsingActivity extends ListActivity {

// url to make request
private static String url = "http://10.0.2.2/product.php";

private static final String TAG_PRODUCTS = " ";
private static final String TAG_ID = "Id";
private static final String TAG_PID = "productId";
private static final String TAG_QTY = "productQty";
private static final String TAG_PRICE = "productPrice";
// contacts JSONArray
JSONArray products = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting Array of Contacts
        products = json.getJSONArray(TAG_PRODUCTS);

        // looping through All Contacts
        for (int i = 0; i < products.length(); i++) {
            JSONObject c = products.getJSONObject(i);

            // Storing each json item in variable
            String id = c.getString(TAG_ID);
            String pid = c.getString(TAG_PID);
            String qty = c.getString(TAG_QTY);
            String price = c.getString(TAG_PRICE);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(TAG_ID, id);
            map.put(TAG_PID, pid);
            map.put(TAG_QTY, qty);

            // adding HashList to ArrayList
            contactList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    /**
     * Updating parsed JSON data into ListView
     * */
    ListAdapter adapter = new SimpleAdapter(this, contactList,
            R.layout.list_item,
            new String[] { TAG_PID, TAG_QTY, TAG_PRICE }, new int[] {
                    R.id.name, R.id.email, R.id.mobile });

    setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();

    // Launching new screen on Selecting Single ListItem
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name))
                    .getText().toString();
            String cost = ((TextView) view.findViewById(R.id.email))
                    .getText().toString();
            String description = ((TextView) view.findViewById(R.id.mobile))
                    .getText().toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(),
                    SingleMenuItemActivity.class);
            in.putExtra(TAG_PID, name);
            in.putExtra(TAG_QTY, cost);
            in.putExtra(TAG_PRICE, description);
            startActivity(in);

        }
    });

}

}

THis is may be usefull for you..

The problem is in your lv.setOnItemClickListener

Change it to

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int arg2,
            long arg3) {
        // on selecting a single album
        // TrackListActivity will be launched to show tracks inside the album
        HashMap<String, String> o=(HashMap<String,String>)lv.getItemAtPosition(Position);       
        Intent i = new Intent(getApplicationContext(), ProductActivity.class);
        i.putExtra("category_name", o.get(category_name));             

        startActivity(i);
    }
});     

Hope this helps..!!

Try like this

    JSONArray arr;
            try {
                arr = new JSONArray("YOUR_JSON");

                for (int i = 0; i < arr.length(); i++) {

                    JSONObject catList = arr.getJSONObject(i);
                    String catName = catList.getString("categoryName");
                    System.out.println("categoryName:" + catName);
                    JSONArray list = catList.getJSONArray("categoryList");
                    for (int j = 0; j < list.length(); j++) {
                        JSONObject subCatList = list.getJSONObject(i);
                        String productID = subCatList.getString("ProductID");
                        String productName = subCatList.getString("ProductName");
                        System.out.println("Sub CatList" + productID + " "
                                + productName);

                    }

                }
            } catch (J

SONException e) {
            e.printStackTrace();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM