我有JsonObject
我有返回JSONArray的方法。我想传递第一个字段并只获取数据数组。然后将其转换为我的阵列列表。我会非常感谢您的任何建议。
void getUsersBeacons(){
type = new TypeToken<List<Beacon>>(){}.getType();
JSONArray myReq = new JSONArray(Request.Method.GET, Url + testId + Url2, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try{
JSONArray buffArray = response.getJSONArray(2);
JSONArray bufJsonArray = response.getJSONArray(1);
beaconsList = converter.fromJson(bufJsonArray.toString(), type);
}catch (Exception e){
e.printStackTrace();
}
}
});
VolleySingleton.getInstance(getActivity().getApplicationContext()).addToRequestQueue(myReq);
}
Beacon课程:
public class Beacon {
private Object idBeacon;
private String friendlyName;
private String imageUrl;
public Beacon(Object idBeacon, String friendlyName, String imageUrl) {
this.idBeacon = idBeacon;
this.friendlyName = friendlyName;
this.imageUrl = imageUrl;
}
public Object getIdBeacon() {
return idBeacon;
}
public void setIdBeacon(Object idBeacon) {
this.idBeacon = idBeacon;
}
public String getFriendlyName() {
return friendlyName;
}
public void setFriendlyName(String friendlyName) {
this.friendlyName = friendlyName;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
答案 0 :(得分:0)
如果我理解正确,你最好传入JSONArray而不是字符串并解析它的内容,如下所示:
internal = TRUE
UPD:在回复您的评论时,您必须使用finalModel
代替public static List<Beacon> fromJson(JSONArray array)
{
ArrayList<Beacon> res = new ArrayList<>();
for (int i = 0; i < array.length(); ++i)
{
JSONObject beacon = array.getJSONObject(i);
res.add(new Beacon(beacon.getInt("beaconId"), beacon.getString("name"), beacon.getString("imageUrl"))));
}
return res;
}
,然后执行此操作:
Response.Listener<JSONObject>