如何使用Retrofit和Gson在Android中解析嵌套的JSON数据

时间:2019-05-07 12:31:26

标签: android json gson retrofit retrofit2

我熟悉使用Retrofit解析基本JSON数据,但为此JSON响应而努力实现正确的POJO对象。

这是JSON数据:

{
    "observations": [
      {
        "id": "0",
        "type": "1st type",
        "data": [
            {
              "name": "some_name",
              "result": "some_result"
            }
          ]
      },
      {
        "id": "1",
        "type": "2nd type",
        "data": [
            {
              "name2": "some_name2",
              "measurement": "some_measurement",
              "field": "some_field",
              "result2": "some_result2"
            }
          ]
      }
    ]
}

我已经为两种观察类型创建了类:

public class DataType1{

    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("measurement")
    @Expose
    private String measurement;
    @SerializedName("field")
    @Expose
    private String field;
    @SerializedName("result")
    @Expose
    private String result;
}

public class DataType2 {
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("result")
    @Expose
    private String result;
}

通常的想法是通过“类型”确定数据的类型并将其发送到相应的类:

if(response.body().getType.equals("1st type"))
{
    Gson gson = new GsonBuilder().create();
    DataType1 data = gson.fromJson(response.body().getObservation, DataType1.class);
}

我认为,接下来我必须创建一个单独的ObservationsResponce类以获取观察列表:

public class Observation {
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("observation")
    @Expose
    private List<??What Should Be Here??> observation = null;
}

但是问题是观察值可以具有不同的数据类型和内部不同的字段。 在这种情况下,此列表应该是哪个类?

2 个答案:

答案 0 :(得分:1)

service

最好使用相同的密钥,如果要使用不同的密钥并希望生成POJO,则最好使用此link

答案 1 :(得分:0)

检查此pojo类 我不确定它是否正常工作

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class Datum {

@SerializedName("name")
@Expose
private String name;
@SerializedName("result")
@Expose
private String result;
@SerializedName("name2")
@Expose
private String name2;
@SerializedName("measurement")
@Expose
private String measurement;
@SerializedName("field")
@Expose
private String field;
@SerializedName("result2")
@Expose
private String result2;

public String getName() {
    return name;
}

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

public String getResult() {
    return result;
}

public void setResult(String result) {
    this.result = result;
}

public String getName2() {
    return name2;
}

public void setName2(String name2) {
    this.name2 = name2;
}

public String getMeasurement() {
    return measurement;
}

public void setMeasurement(String measurement) {
    this.measurement = measurement;
}

public String getField() {
    return field;
}

public void setField(String field) {
    this.field = field;
}

public String getResult2() {
    return result2;
}

public void setResult2(String result2) {
    this.result2 = result2;
}

}

class Example {

@SerializedName("observations")
@Expose
private List<Observation> observations = null;

public List<Observation> getObservations() {
    return observations;
}

public void setObservations(List<Observation> observations) {
    this.observations = observations;
}

}

class Observation {

@SerializedName("id")
@Expose
private String id;
@SerializedName("type")
@Expose
private String type;
@SerializedName("data")
@Expose
private List<Datum> data = null;

public String getId() {
    return id;
}

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

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

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

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

}