本文介绍了Android Retrofit POJO模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习android,现在为其余的POST和GET请求实现了1.9改造,有人可以帮助我如何为给定的json对象和字符串建模吗?我对一些教程感到非常困惑,我已经学会了如何为此json对象制作pojo

Hi i'm trying to learn android and now implementing the retrofit 1.9 for my rest POST and GET request can somebody help me on how to model given json objects and strings? im very confused on some tutorials I have learned how make a pojo for this json object

{
"contacts": [
    {
            "id": "c200",
            "name": "Ravi Tamada",
            "email": "[email protected]",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c201",
            "name": "Johnny Depp",
            "email": "[email protected]",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    }
    }}]}

使用此模型

Contacts.class

Contacts.class

public class Contacts {
@SerializedName("contacts")
@Expose
private List<Contact> contacts = new ArrayList<Contact>();

public List<Contact> getContacts() {
    return contacts;
}

public void setContacts(List<Contact> contacts) {
    this.contacts = contacts;
}

对象的Contact.class

and Contact.class for the objects

public class Contact {

@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("email")
@Expose
private String email;
@SerializedName("address")
@Expose
private String address;
@SerializedName("gender")
@Expose
private String gender;

public String getId() {return id;}
public void setId(String id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public String getEmail() {return email;}
public void setEmail(String email) {this.email = email;}
public String getAddress() {return address;}
public void setAddress(String address) {this.address = address;}
public String getGender() {return gender;}
public void setGender(String gender) {this.gender = gender;}}

然后在MainActivity.class上使用此调用列表

And Calling the list using this on my MainActivity.class

   private void getContacts() {
    final ProgressDialog loading = ProgressDialog.show(this, "Fetching Data", "Please wait...", false, false);

    RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ROOT_URL).build();
    ContactsAPI api = adapter.create(ContactsAPI.class);
    api.getContacts(new Callback<Contacts>() {
        @Override
        public void success(Contacts contacts, Response response) {
            loading.dismiss();
            List<Contact> contactList = contacts.getContacts();
            String[] items = new String[contactList.size()];

            for (int i = 0; i < contactList.size(); i++) {

                items[i] = contactList.get(i).getName();
            }
            ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.simple_list,R.id.textview, items);
            //Setting adapter to listviesw
            listView.setAdapter(adapter);
        }
        @Override
        public void failure(RetrofitError error) {
        }
    });
}

然后我的问题是我应该如何用这个数组Object创建模型?

THen my Question is how should i make a model out of this array Object?

{
"-KNea90tV5nZlkeqxc3Q": {
    "accountName": "Mark Papyrus",
    "accountNumber": "12435656443",
    "accountType": "Peso Savings"
},
"-KNeaPmBoTXV4mQC6cia": {
    "accountName": "Mark Dremeur",
    "accountNumber": "12435656444",
    "accountType": "Peso Checking"
}

我发现如何制作模型和给定json数组的差异令人困惑,请指导我.

i found it confusing how to make models and difference of given json arrays pls guide me thanks.

推荐答案

如果键"KNea90tV5nZlkeqxc3Q"是动态的并且需要捕获它们,则必须在模型中使用哈希映射才能正确捕获它们.

if the key "KNea90tV5nZlkeqxc3Q" is dynamic and need to capture them, you musto to use a hashmap in your model to catch them correctly.

检查此问题可能会有用:

check this issue it could be useful:

使用翻新功能解析动态关键字Json字符串

这篇关于Android Retrofit POJO模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:12