首页 > 解决方案 > 在 android retrofit2 中解析 JSON 数据

问题描述

我正在尝试使用 android 中的改造来解析 json 数据。

json数据:

{
    "data": [
        {
            "id": "9",
            "type": "vehicle",
            "attributes": {
                "name": "Audi",
                "vehicle_type": "Self Driven",
                "registration": "KA2510101",
                "image": "/uploads/vehicle/image/9/audi.jpeg"
            }
        },
        {
            "id": "7",
            "type": "vehicle",
            "attributes": {
                "name": "BMW",
                "vehicle_type": "Self Driven",
                "registration": "KA23MN9806",
                "image": "/uploads/vehicle/image/7/low.time1447943958802.jpg"
            }
        }
}

在这里,我想检索注册数据。

数据模型类

public class PostUserLogin {

    @SerializedName("email")
    String email;
    @SerializedName("password")
    String password;
    @SerializedName("designation")
    String designation;
    @SerializedName("token")
    String token;
    @SerializedName("data")
    ArrayList<Data> data;

    public ArrayList<Data> getData() {
        return data;
    }

    public void setData(ArrayList<Data> data) {
        this.data = data;
    }
    public PostUserLogin(String email, String password, String designation) {
        this.email = email;
        this.password = password;
        this.designation = designation;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDesignation() {
        return designation;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
    }

    public class Data{
        @SerializedName("id")
        String id;
        @SerializedName("attributes")
        String attributes;
        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }
        public String getAttributes() {
            return attributes;
        }

        public void setAttributes(String attributes) {
            this.attributes = attributes;
        }
    }
    public class Registration{
        @SerializedName("registration")
        String regNum;
        public String getRegNum() {
            return regNum;
        }

        public void setRegNum(String regNum) {
            this.regNum = regNum;
        }
    }

}

解析json数据方法:

  Call<PostUserLogin> call = apiInterface.set_token(token);
        call.enqueue(new Callback<PostUserLogin>() {
            @Override
            public void onResponse(Call<PostUserLogin> call, Response<PostUserLogin> response) {
                ArrayList<PostUserLogin.Data> arrayList = response.body().getData();
                if(arrayList != null){
                 for(int i=0;i<arrayList.size();i++){
                     Log.d("retrofit","vehicleId = "+arrayList.get(i).getId());
                 }
                }
            }

            @Override
            public void onFailure(Call<PostUserLogin> call, Throwable throwable) {

            }
        });

当我试图获取 id 时,它工作正常。注册时无法获取任何数据。如何从给定的 json 数据中获取注册数据。

标签: androidjson

解决方案


    @SerializedName("attributes")
    String attributes;

你正在使用String它应该是一个类

public class Attributes {

    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("vehicle_type")
    @Expose
    private String vehicleType;
    @SerializedName("registration")
    @Expose
    private String registration;
    @SerializedName("image")
    @Expose
    private String image;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVehicleType() {
        return vehicleType;
    }

    public void setVehicleType(String vehicleType) {
        this.vehicleType = vehicleType;
    }

    public String getRegistration() {
        return registration;
    }

    public void setRegistration(String registration) {
        this.registration = registration;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

}

并在您的数据类中使用以下

@SerializedName("attributes")
@Expose
private Attributes attributes;

推荐阅读