Android Studio将JSON变成对象列表

发布于 2025-01-17 14:37:14 字数 1293 浏览 0 评论 0原文

我目前正在尝试将JSON对象带入对象类中,然后将这些对象存储在列表中。我似乎能够将字符串值分配给变量,但是INT和双重值不起作用。

我的代码是:

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

         }

当JsonObject jsondata通过for循环运行时(jsonArray是对象的数组),json看起来像这样:

{“ doormat_id”:“ 176”,“ latitude”,“ latitude”:“ “,“经度”:“ - 82.50953674316406”,“ create_by”:“ user”,“ shape”:“ default_shape”,“ color”,“ color”:“ default_color”}

我是Android开发的新手,所以提供帮助将不胜感激。谢谢!

我试图为诸如“ toormat_id”之类的值拉一个字符串,但是它们返回空白,我无法将它们解析为int。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

行至春深 2025-01-24 14:37:14

您可以使用 GSON 库将 json 数据转换为类对象。在 build.gradle(:app) 中提到这个库。

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

然后在活动中像这样使用它:

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

现在一些解释,假设我们有一个像这样的 json 对象:

  {
    "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"
        }
    ]
}

现在创建这个 json 对象名称 UserData 的 Model 类:

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

现在您可以使用上面提到的 Gson 代码将 json 数据解析为 UserData 类并然后从 user_data.getData() 获取数组列表;

Ps = 如果您想获取字符串中的所有 json 参数(例如 =doormat_id、经度等),则在 Model 类中将它们声明为 String,GSON 库将处理其余部分。

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.

¢蛋碎的人ぎ生 2025-01-24 14:37:14

请使用以下语句:

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

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"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文