0

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": "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"
            }
    }
    }}]}

Using this model

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

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

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

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"
}

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

user3262438
  • 73
  • 1
  • 8
  • 2
    Here's a protip - generate it using http://www.jsonschema2pojo.org/ for example. – Shark Aug 01 '16 at 08:52
  • @Shark- Actually done that but my problem is the json Keys like this "-KNea90tV5nZlkeqxc3Q" is random generated so i cant make it constant – user3262438 Aug 01 '16 at 09:19
  • 1
    That would make JSON a bad choice, unless the randomly generated salt part becomes a member. I'm saying it's bad because the model class would end up containing all the random values and assigning value to just one of them, without you knowing which one to check... it warrants a talk with the backend team to give you further instructions. – Shark Aug 01 '16 at 09:22
  • @Shark - sadly i can't do that because i need to be dynamic for my handling – user3262438 Aug 02 '16 at 08:19
  • Ouch... Good luck. – Shark Aug 02 '16 at 08:24

2 Answers2

0

I think me and you are having the same problems but I am stuck so I can help you as far as I can . First you cannot use jsonschema2pojo.org to create the pojo class you need to use hashmap from what I understand "-KNea90tV5nZlkeqxc3Q": is a key but 1 of the classes should be the following ( also the accountype since its a type you can use enum but it would be bit harder to code)

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

 }
user3277530
  • 351
  • 6
  • 14
0

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:

Parse Dynamic Key Json String using Retrofit

Ruben Caster
  • 290
  • 1
  • 5
  • 15