my challenge is that I find it difficult and tricky to Format string to a json data such as this:
{
"contacts":[
{
"displayName" : "Michael"
},
{
"displayName" : "Efe",
"phoneNumbers" : [
{
"value" : "+23470390989"
}
]
},
{
"displayName" : "Efe6",
"phoneNumbers" : [
{
"value" : "+2347002478"
}
]
},
{
"displayName" : "No Reg",
"phoneNumbers" : [
{
"value" : "+2347034567890"
}
]
},
{
"displayName" : "Efe2",
"phoneNumbers" : [
{
"value" : "09058528818"
}
]
},
{
"displayName" : "Whales",
"phoneNumbers" : [
{
"value" : "+23490574583"
},
{
"value" : "+23481847979"
}
]
}
]
}
and the string I'm trying to format like that is coming from a Getcontact Class(It get's list of contacts from the phone), hopefully many people are familiar with that method for getting contacts from the mobile device.
TRIED
What I have tried so far is that:
ArrayList<PhoneNuberStructure> phoneNuberStructures = new ArrayList<>();
phoneNuberStructures.add(/*arrays of phonenumbers will come here*/);
AND
ContactsStructure contactsStructure= new ContactsStructure();
contactsStructure.setDisplayName(name);
contactsStructure.setPhoneNumbers(new PhoneNuberStructure);
SO THIS
ArrayList<ContactsStructure> contacts = new ArrayList<ContactsStructure>();
contacts.add(contactsStructure);
but I'm not really getting it right! and it's confusing...
Any help will be nice. Thank you all.
Gson gson = new GsonBuilder().create(); gson.toJson(topLevelObject);. You have to create a new array for each break;