23

我正在使用我的 json 对象返回一组结果,并且我正在尝试使用我的 customObjectResponse 类来提取每个对象中的每个字段......它期望一个对象的问题那么我该如何编辑我的类允许它接收一个对象数组以便能够调用每个对象的字段......我对需要添加的内容感到困惑:

这是传递以供使用的响应示例:

[
  {
    itemId: 'dfsdfsdf343434',
    name: 'tests',
    picture: '6976-7jv8h5.jpg',
    description: 'testy.',
    dateUpdated: 1395101819,

  }
]

这是我的响应对象类:

public class ObjResponse{
    private String itemId;
    private String name;
    private String picture;

    private String description;

    private String location;
    private int dateUpdated;

    private String msg;




    //gridview constructor
    public ObjResponse(String picture) {
        this.picture = picture;
    }

    //public constructor
    public ObjResponse() {

    }

    public String getItemId() {
        return itemId;
    }

    public void setItemId(String itemId) {
        this.itemId = itemId;
    }

    public String getName() {
        return name;
    }

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

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }


    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }


    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }



    public int getDateUpdated() {
        return dateUpdated;
    }

    public void setDateUpdated(int dateUpdated) {
        this.dateUpdated = dateUpdated;
    }




    public String getMsg() {
        return msg;
    }

}

我正在尝试,但没有工作,即使我将这些类分成自己的文件:

Data passed in:
items: [{obj1: "A", obj2: ["c", "d"]}, {etc...}]


public class Response {

        public class List<Custom> {
                private List<Custom> items;
        }

        public class Custom {
                private String obj1;
                private List<Obj2> obj2;
        }

        public Class Obj2 {
                private String letters;
        }
}
4

4 回答 4

34

我最终只是在回调中调用了一个 customObject 列表,它完成了这项工作......

new Callback<List<ObjResponse>>() {
于 2014-03-18T12:39:26.643 回答
13

我最初很难了解如何OP解决他的问题,但经过几天的调试,我终于想出了如何解决这个问题。

所以你基本上有这样格式的数据(JSON 对象的 JSON 数组):

[
    {
      ...
    }
] 

您对数据进行建模并包含 getter 和 setter 方法的类只不过是您的典型 POJO。

public class Person implements Serializable {
    @SerializedName("Exact format of your json field name goes here")
    private String firstName;

    // Getters and Setters....
}

在包含 RESTful 注释的界面中,您希望将呼叫从以下位置转换:

前:

public interface APInterface {
    @GET("SOME URL TO YOUR JSON ARRAY")
    Call<Person>(...)
}

后:

public interface APInterface {
    @GET("SOME URL TO YOUR JSON ARRAY")
    Call<List<Person>>(...)
}

在您的 android 活动中,您希望将所有调用转换Call<Person>Call<List<Person>>

最后,在进行初始异步请求调用时,您将希望像这样转换回调。

call.enqueue(new Callback<List<Person>>() {
    @Override
    public void onResponse(Call<List<Person>> call, Response<List<Person>> response) {

        if(response.isSuccessful()){
            List<Person> person = response.body();

           // Can iterate through list and grab Getters from POJO
           for(Person p: person){...}


        } else {
            // Error response...
        }

    }

    @Override
    public void onFailure(Call<List<Person>> call, Throwable t) {...}
});

希望这可以帮助那些从上面接受的答案中迷失的其他人。

于 2016-09-29T15:23:44.203 回答
4

This can also work by just passing an array of response objects. So if this is your response object:

public class CustomUserResponse {
    public String firstName;
    public String lastName;
    ...
}

You can use related syntax, depending on how you use the callbacks. Such as:

new Callback<CustomUserResponse[]>(){
    @Override
    public void success(CustomUserResponse[] customUserResponses, Response rawResponse) {

    }

    @Override
    public void failure(RetrofitError error) {

    }
};

OR

public class GetUserCommand implements Callback<CustomUserResponse[]> { ...

Put simply, in every place where you normally replace T with a response class, replace it with an array, instead as in CustomUserResponse[].


NOTE: to avoid confusing errors, be sure to also use an array in the Retrofit interface definition:

@POST ( "/users" )
public void listUsers(@Body GetUsersRequest request, Callback<CustomUserResponse[]> callback);
于 2014-11-10T17:48:40.807 回答
2

你可以试试这样的

JSONObject jsonObject = new JSONObject(<your JSON string result>);
JSONArray jsonArray = jsonObject.getJSONArray();

//use GSON to parse
if (jsonArray != null) {
   Gson gson = new Gson();
   ObjResponse[] objResponse = gson.fromJson(jsonArray.toString(), ObjResponse[].class);
   List<ObjResponse> objResponseList = Arrays.asList(objResponse);
}

这绝对应该有效。

于 2014-03-18T02:40:09.490 回答