改进了预期的BEGIN_ARRAY但是BEGIN_OBJECT异常

时间:2016-10-05 08:26:20

标签: json retrofit

我知道有一些类似的问题,但没有一个与我的问题相符。我有来自服务器的JSON响应,如下所示:

{
  "data": {
    "list": {
      "beacons": [
        {
          "id": 56,
          "beacon_id": "56",
          "bridge_id": null,
          "mesh_id": null,
          "name": "Test beacon wcx",
          "location": "test",
          "mac_address": "e6:f5:8a:94:b8:c7",
          "firmware": "1.1",
          "pcb_revision": "2.0",
          "battery_level": null,
          "password": "0000",
          "ib": {
            "turn_on": "1",
            "major": "300",
            "minor": "10",
            "tx_power": "4",
            "interval": "400",
            "uuid": "dae19960-18ea-11e5-8ff0-0002a5d5c521",
            "secure_uuid": "0"
          },
          "euid": {
            "turn_on": "0",
            "namespace": null,
            "instance": null,
            "tx_power": null,
            "interval": null
          },
          "eeid": {
            "turn_on": "0",
            "tx_power": null,
            "interval": null
          },
          "eurl": {
            "turn_on": "0",
            "tx_power": null,
            "interval": null,
            "url": null
          },
          "etlm": {
            "turn_on": "0",
            "tx_power": null,
            "interval": null
          }
        },
        {
          "id": 57,
          "beacon_id": "57",
          "bridge_id": "13",
          "mesh_id": "278",
          "name": "Marta B",
          "location": "lk",
          "mac_address": "e1:4b:64:c3:80:e2",
          "firmware": "2.1",
          "pcb_revision": "2.0",
          "battery_level": "97",
          "password": "0000",
          "ib": {
            "turn_on": "1",
            "major": "300",
            "minor": "6",
            "tx_power": "0",
            "interval": "0",
            "uuid": "dae19960-18ea-11e5-8ff0-0002a5d5c521",
            "secure_uuid": "0"
          },
          "euid": {
            "turn_on": "0",
            "namespace": null,
            "instance": null,
            "tx_power": null,
            "interval": null
          },
          "eeid": {
            "turn_on": "0",
            "tx_power": null,
            "interval": null
          },
          "eurl": {
            "turn_on": "0",
            "tx_power": null,
            "interval": null,
            "url": null
          },
          "etlm": {
            "turn_on": "0",
            "tx_power": null,
            "interval": null
          }
        },
        {
          "id": 58,
          "beacon_id": "58",
          "bridge_id": null,
          "mesh_id": null,
          "name": "Marta C",
          "location": "abcdlk",
          "mac_address": "3c:cf:82:8a:e7:fe",
          "firmware": "2.1",
          "pcb_revision": "2.0",
          "battery_level": "99",
          "password": "0000",
          "ib": {
            "turn_on": "1",
            "major": "300",
            "minor": "39",
            "tx_power": "7",
            "interval": "400",
            "uuid": "dae19960-18ea-11e5-8ff0-0002a5d5c521",
            "secure_uuid": "0"
          },
          "euid": {
            "turn_on": "0",
            "namespace": null,
            "instance": null,
            "tx_power": null,
            "interval": null
          },
          "eeid": {
            "turn_on": "0",
            "tx_power": null,
            "interval": null
          },
          "eurl": {
            "turn_on": "0",
            "tx_power": null,
            "interval": null,
            "url": null
          },
          "etlm": {
            "turn_on": "0",
            "tx_power": null,
            "interval": null
          }
        }
      ],
      "bridges": [
        {
          "id": 13,
          "name": "Test bridge wcx",
          "location": "netizens",
          "mac_address": "c9:1d:76:cc:a7:ca",
          "ib": {
            "turn_on": 1,
            "major": "100",
            "minor": "102",
            "tx_power": "6",
            "interval": "400",
            "uuid": "dae19960-18ea-11e5-8ff0-0002a5d5c521",
            "secure_uuid": 0
          },
          "euid": {
            "turn_on": "0",
            "namespace": null,
            "instance": null,
            "tx_power": null,
            "interval": null
          },
          "eeid": {
            "turn_on": "0",
            "tx_power": null,
            "interval": null
          },
          "eurl": {
            "turn_on": "0",
            "tx_power": null,
            "interval": null,
            "url": null
          },
          "etlm": {
            "turn_on": "0",
            "tx_power": null,
            "interval": null
          }
        }
      ]
    }
  },
  "ver": 1,
  "time": 1475576646,
  "status": 1,
  "status_msg": ""
}

我的模型类看起来像这样:

public class BeaconResponse {
@Expose
public DataBeacon dataBeacon;
@Expose
public int ver;
@Expose
public long time;
@Expose
public int status;
@Expose
public String status_msg;

DataBeacon包含信标和桥接对象列表:

@Table(name = "dataBeacon", id = "_id")

public class DataBeacon extends Model {
@Column(name = "beacon",onUpdate = Column.ForeignKeyAction.CASCADE, onDelete = Column.ForeignKeyAction.CASCADE)
public List<BeaconsModel> beacons;
@Column(name = "bridge",onUpdate = Column.ForeignKeyAction.CASCADE, onDelete = Column.ForeignKeyAction.CASCADE)
public List<BridgeModel> bridges;

}

这是我获取json的界面:

@GET("/api/mobile/{language}/{apiVersion}/beacons/list")
    void beaconsList(Callback<BasicResponse<BeaconsListResponse>> callback);

public class BeaconsListResponse {
    @Expose
    public List<BeaconResponse> list;
}

在我看来,一切都应该正常工作,但我仍然有例外:Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 18 path $.data.list。我不知道为什么 - 模型的结构应该是正确的。

1 个答案:

答案 0 :(得分:2)

您的模型与JSON响应不匹配,JSON响应返回一个对象,但是您的模型期望列表,因此错误。

我认为您应该按照以下方式重新设计模型:

public class BeaconResponse
{
    private Data data;
    public int ver;
    public long time;
    public int status;
    public String status_msg;
}

public class Data
{
    private List list;
}

public class List
{
    private Beacons[] beacons;
    private Bridges[] bridges;
}

然后你应该更新你的改装界面以期望BeaconResponse而不是BeaconListResponse

void beaconsList(Callback<BasicResponse<BeaconsResponse>> callback);

请注意我没有提供完整详细的模型,只是想解释一下如何将模型与JSON响应相匹配。只需继续添加模型详细信息即可测试您的应用,并告知我们是否有效。