Android Retrofit POJO模型

时间:2016-08-01 08:49:32

标签: java android json model retrofit

您好我正在尝试学习android并且现在为我的其余POST和GET请求实现改进1.9可以有人帮助我如何建模给定的json对象和字符串?我在一些教程上非常困惑,我学会了如何为这个json对象制作一个pojo

{
"contacts": [
    {
            "id": "c200",
            "name": "Ravi Tamada",
            "email": "ravi@gmail.com",
            "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": "johnny_depp@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    }
    }}]}

使用此模型

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

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上使用此列表调用列表

   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) {
        }
    });
}

我的问题是如何从这个数组对象中创建一个模型?

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

我发现令人困惑的是如何制作模型以及给定json数组的差异请指导我。

2 个答案:

答案 0 :(得分:0)

我认为我和你有同样的问题,但我被困住所以我可以尽我所能帮助你。首先你不能使用jsonschema2pojo.org来创建你需要使用hashmap的pojo类,我理解&#34; -KNea90tV5nZlkeqxc3Q&#34;:是一个键,但其中一个类应该是以下(也是帐户类型,因为它的类型你可以使用枚举,但它会更难编码)

public class Account {




private String accountName;

private String accountNumber;

private String accountType;


public Account() {
                  }




public String getAccountName() {return accountName;}
public void setAccountName(String accountName) {
 this.accountName=accountName;}

// *** repeat for the accountnumber and accountype

 }

答案 1 :(得分:0)

如果键“KNea90tV5nZlkeqxc3Q”是动态的并且需要捕获它们,则必须在模型中使用散列映射来正确捕获它们。

检查此问题可能有用:

Parse Dynamic Key Json String using Retrofit