javaandroidjsonandroid-json

Fetch data of JSON array on Android


I have a few test values in my database and i want to fetch them all in android. This is my following JSON output of the values inside my Database:

 [{"email_address":"some@email.com","comment":"qwehgashdgaskdaweq","date_comment":"2014-06-21","time_comment":"08:28:00","password":"somePass"},
 {"email_address":"some@email.com","comment":"asfasdasdasd","date_comment":"2104-06-12","time_comment":"09:03:00","password":"somePass"}
 {"email_address":"some@email.com","comment":"asdsfafd","date_comment":"2014-06-22","time_comment":"04:44:00","password":"somePass"}]

But every time that I run this code: http://prntscr.com/3vhs5j , it only gives me the first line of the JSON output.: http://prntscr.com/3vi34b

How can I show all the rows( instead of 1 row)in android?


Solution

  • You can use a JSONObject provided by the android API. The JSONObject has a method called getJSONArray()

    http://developer.android.com/reference/org/json/JSONObject.html#getJSONArray(java.lang.String)

    your error there is that you are instantiating a JSONArray with your Object. you should try this:

    JSONArray myArray =jsonObject.getJSONArray();

    This is a snippet of code from one of my apps that worked very well to retrieve Json data.

     success = jObject.getInt(TAG_SUCCESS);
     vehicles = jObject.getJSONArray(Vehicle.TAG_VEHICLES);
    
            if(success == 1) {
                // loop through all the vehicles
                for(int i = 0; i < vehicles.length(); i++) {
                    JSONObject obj = vehicles.getJSONObject(i);
    
                    // Get each element based on it's tag
                    String year = obj.getString(Vehicle.TAG_YEAR);
                    String model = obj.getString(Vehicle.TAG_MODEL);
                    String brand = obj.getString(Vehicle.TAG_BRAND);
                    String color = obj.getString(Vehicle.TAG_COLOR);
                    String license_plate = obj.getString(Vehicle.TAG_LICENSE);
                    String main_driver = obj.getString(Vehicle.TAG_DRIVER);
                    String policeNumber = obj.getString(Vehicle.TAG_POLICENUMBER);
                    String driversLicense = obj.getString(Vehicle.TAG_DRIVER_LICENSE);
                    String licenseState = obj.getString(Vehicle.TAG_LICENSE_STATE);
                    String driverBirthday = obj.getString(Vehicle.TAG_BIRTHDAY_MONTH) + "/" +
                            obj.getString(Vehicle.TAG_BIRTHDAY_DAY) + "/" +
                            obj.get(Vehicle.TAG_BIRTHDAY_YEAR);
                    String driverGender = obj.getString(Vehicle.TAG_DRIVER_GENDER);
    
                    ListRowGroup group = new ListRowGroup(brand + " " + year + " " + model, brand);
                    group.children.add(brand + " " + year + " " + model);
                    group.children.add(policeNumber);
                    group.children.add(model);
                    group.children.add(color);
                    group.children.add(license_plate);
                    group.children.add(main_driver);
                    group.children.add(driversLicense + "   -   " + licenseState);
                    group.children.add(driverBirthday + "   -   " + driverGender);
    
                    vehiclesGroup.append(i, group);
                }