如何从json对象中获取对象?

时间:2019-04-16 10:50:38

标签: android json retrofit2

我正在使用Retrofit创建GET方法,并且需要在另一个对象内部获取一个名为“ bus”的对象,我可以将其称为“ BusInspectionReport”,使用id作为查询来获取它的正确编码方法是什么?

我要从json中仅获取“ bus”对象的内容。我创建了一个名为BusInspectionReport的类,其参数之一是Bus类的'bus'对象。总线类将代表json文件中“总线”对象的内容。

id = 1的json文件示例:

{
    "response_code":5,
    "response_msg":"Success",
    "bus":{
        "vehicle_id":"1",
        "vehicle_plate":"ا ا ا 4389",
        "category":"ورشة",
        "category_id":2,
        "last_seen":"2019-04-15T12:19:24.4223277",
        "location":{
            "lat":0.0,
            "lon":0.0
        },
        "status":{
            "sos":false,
            "ignition":"",
            "movement":false,
            "speed":0.0
        },
            "driver":{
                "driver_id":0,
                "driver_name":""
            },
            "inspection_report":[]
        }
}

BusInspectionReport.java:

public class BusInspectionReport {

    private int response_code;
    private String response_msg;
    private Bus bus;

    public BusInspectionReport(int response_code, String response_msg, Bus bus) {
        this.response_code = response_code;
        this.response_msg = response_msg;
        this.bus = bus;
    }

    public int getResponse_code() {
        return response_code;
    }

    public void setResponse_code(int response_code) {
        this.response_code = response_code;
    }

    public String getResponse_msg() {
        return response_msg;
    }

    public void setResponse_msg(String response_msg) {
        this.response_msg = response_msg;
    }

    public Bus getBus() {
        return bus;
    }

    public void setBus(Bus bus) {
        this.bus = bus;
    }
}

Bus.java:

public class Bus {

    private String vehicle_id;
    private String vehicle_plate;
    private String category;
    private String category_id;
    private String last_seen;
    private Status status;
    private Driver driver;

    public Bus(String vehicle_id, String vehicle_plate, String category, String category_id, String last_seen, Status status, Driver driver) {
        this.vehicle_id = vehicle_id;
        this.vehicle_plate = vehicle_plate;
        this.category = category;
        this.category_id = category_id;
        this.last_seen = last_seen;
        this.status = status;
        this.driver = driver;
    }

    public String getVehicle_id() {
        return vehicle_id;
    }

    public void setVehicle_id(String vehicle_id) {
        this.vehicle_id = vehicle_id;
    }

    public String getVehicle_plate() {
        return vehicle_plate;
    }

    public void setVehicle_plate(String vehicle_plate) {
        this.vehicle_plate = vehicle_plate;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getCategory_id() {
        return category_id;
    }

    public void setCategory_id(String category_id) {
        this.category_id = category_id;
    }

    public String getLast_seen() {
        return last_seen;
    }

    public void setLast_seen(String last_seen) {
        this.last_seen = last_seen;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public Driver getDriver() {
        return driver;
    }

    public void setDriver(Driver driver) {
        this.driver = driver;
    }
}

MyApi.java:

public interface MyApi {

    String BASE_URL = "/*BASE URL*/";

    @GET("/*API PATH*/")
    Call<Bus> getBus(@Query("id") String vehicle_id);

}

我不确定GET方法是否正确,我应该首先获取BusInspectionReport对象以获得“ bus”对象吗?还是我可以直接乘坐“公共汽车”?

1 个答案:

答案 0 :(得分:0)

Call<Bus>更改为Call<BusInspectionReport>,然后从Bus对象获得BusInspectionReport

public interface MyApi {
    String BASE_URL = "/*BASE URL*/";
    @GET("/*API PATH*/")
    Call<BusInspectionReport> getBus(@Query("id") String vehicle_id);

}