问题描述
我要看书JSON数组,它具有以下格式: [{vehicle_id:76,色:红},{vehicle_id:7 ,色:蓝}]
,以下Robospice的的。
I'm trying to read JSON array, which has the following format: [{"vehicle_id":"76","color":"red"},{"vehicle_id":"7","color":"blue"}]
, following Robospice's Starter Guide.
Vehicle.java
public class Vehicle {
@JsonProperty("vehicle_id")
private int vehicleID;
@JsonProperty("color")
private String color;
}
(getter和setter方法跟踪)
(setters and getters follow)
类,它是给错误:
VehiclesRequest.class
public class VehiclesRequest extends SpringAndroidSpiceRequest<Vehicle> {
private static final String METHOD = "systemVehicles";
public SystemVehiclesRequest() {
super(Vehicle.class);
}
@Override
public Vehicle[] loadDataFromNetwork() throws Exception {
return getRestTemplate().getForObject(
FULL_URL,
Vehicle[].class);
}
}
正如你所看到的,我重写 loadDataFromNetwork()
方法,然后将它传递给 spiceManager.execute()
我的活动里面的方法和使用自定义监听请求读取数据。 但是我不能返回和阵列(我试过用列表与LT;&GT;
太)从 loadDataFromNetwork()
,你会在这里是最好的解决办法吗我知道我可以以不同的方式获得数据,但我想还是用我的监听器,并能够使尝试
块的方式。
As you can see, I'm overriding loadDataFromNetwork()
method and then passing it to the spiceManager.execute()
method inside my activity and reading data from the request using custom listener. However I cannot return and array (I've tried with the List<>
too) from loadDataFromNetwork()
, what would be the best workaround here? I know I could get the data in a different way but I want to still use my listener and be able to make try
block that way.
推荐答案
我加入的问题解决了车
类:
I solved the problem by adding Vehicles
class:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Vehicles extends ArrayList<Vehicle> {
public Vehicles() {
}
}
和不断变化的 loadDataFromNetwork()
方式类似:
and changing loadDataFromNetwork()
method like that:
public Vehicles loadDataFromNetwork() throws Exception {
return getRestTemplate().getForObject(
FULL_URL,
Vehicles.class);
}
这篇关于杰克逊 - 使用loadDataFromNetwork阅读JSON数组Robospice()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!