Home > Back-end >  Android Studio turning JSON into list of objects
Android Studio turning JSON into list of objects

Time:03-28

I'm currently trying to take a JSON object and store the values into an object class, then store those objects in a list. I seem to be able to assign the string values to variables, but the int and double values are not working.

My code is:

        JSONObject json = new JSONObject(result);

        ArrayList<Doormat> doormats = new ArrayList<>();

        JSONArray jsonArray = json.optJSONArray("data");


        for(int i = 0; i < Objects.requireNonNull(jsonArray).length(); i  ){
            JSONObject jsonData = jsonArray.optJSONObject(i);

            int doormat_id = jsonData.optInt("doormat_id");

            double latitude = jsonData.optDouble("latitude");
            double longitude = jsonData.optDouble("longitude");

            String created_by = jsonData.optString("created_by");
            String shape = json.optString("shape");
            String color = json.optString("color");

            Doormat doormat = new Doormat(doormat_id, latitude, longitude, created_by, shape, color);
            doormats.add(doormat);

         }

By the time the JSONObject jsonData is running through the for loop (the JSONArray is an array of objects), the JSON looks like this:

{"doormat_id":"176", "latitude":"28.135974884033203", "longitude":"-82.50953674316406", "created_by":"User", "shape":"default_shape", "color":"default_color" }

I'm pretty new to Android development, so any help with this would be greatly appreciated. Thanks!

I've tried to pull a String for values like "doormat_id", but they return blank and I cannot parse them to an int.

CodePudding user response:

Please use below statements.

Double.parseDouble(jsonData.optString("latitude")) or Double.valueOf(jsonData.optString("latitude")).doubleValue();

--------->

Integer.parseInt(jsonData.optString("doormat_id")) or Integer.valueOf(jsonData.optString("doormat_id"));

CodePudding user response:

You can use GSON Library to convert json data to class object. In build.gradle(:app) mention this library.

implementation 'com.google.code.gson:gson:2.8.6'

then in activity use it like this:

JSONObject obj = new JSONObject(result);
UserData user_data = (UserData) new Gson().fromJson(obj.toString(), UserData.class);

Now some explanation, suppose we have a json object like this :

  {
    "data": [{
            "doormat_id": 12,
            "latitude": 72.15642,
            "longitude": 72.15642,
            "created_by": "Batman",
            "shape": "mascular",
            "color": "black"
        },
        {
            "doormat_id": 13,
            "latitude": 72.15642,
            "longitude": 72.15642,
            "created_by": "Superman",
            "shape": "mighty",
            "color": "red"
        }
    ]
}

Now create a Model class of this json object name UserData :

   public class UserData {

    private List<DataBean> data;

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        private int doormat_id;
        private double latitude;
        private double longitude;
        private String created_by;
        private String shape;
        private String color;

        public int getDoormat_id() {
            return doormat_id;
        }

        public void setDoormat_id(int doormat_id) {
            this.doormat_id = doormat_id;
        }

        public double getLatitude() {
            return latitude;
        }

        public void setLatitude(double latitude) {
            this.latitude = latitude;
        }

        public double getLongitude() {
            return longitude;
        }

        public void setLongitude(double longitude) {
            this.longitude = longitude;
        }

        public String getCreated_by() {
            return created_by;
        }

        public void setCreated_by(String created_by) {
            this.created_by = created_by;
        }

        public String getShape() {
            return shape;
        }

        public void setShape(String shape) {
            this.shape = shape;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }
    }
}

Now you can use the above mentioned Gson code to parse json data to UserData class and then get the array list from user_data.getData();

P.s = if you want to get all of json parameter (like = doormat_id,longitude,etc) in string then in Model class declare them as String, GSON library will handle the rest.

  • Related