Reputation: 1806
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.
Upvotes: 0
Views: 76
Reputation: 1806
I just have to stick to this for clearity. THUMBS UP to all response
/*CODE SECTION 1*/
JSONObject con = new JSONObject();
JSONArray contacts = new JSONArray();
/*CODE SECTION 2*/
JSONObject contactInfo = new JSONObject();
contactInfo.put("displayName" , name);
JSONArray phoneNos = new JSONArray();
JSONObject value = new JSONObject();
value.put("value" , phoneNo);
phoneNos.put(value);
contactInfo.put("phoneNumbers" , phoneNos);
contacts.put(contactInfo);
/*CODE SECTION 3*/
con.put("contacts", contacts);
So what actually happens is that due to the way i'm fetching my contacts from phone book, makes it easy to use that code. Below is the final look of my code for getting contacts from phone and returning it in the REQUESTED JSON FORMAT. So the main setups happen in CODE SECTION 2.
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
/* INSERT CODE SECTION 1*/
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
/* INSERT CODE SECTION 2*/
}
pCur.close();
}
}
}
/*INSERT CODE SECTION 3*/
Upvotes: 0
Reputation: 7421
If I understood it properly what you are trying to ask, you classes should look like this:
class Contact {
String displayName;
ArrayList<PhoneNumber> phoneNumbers;
// constructors and getter/setters
}
class PhoneNumber {
String value;
// constructor and getter/setters
}
Now, you need to create an array of contacts.
ArrayList<Contact> contacts = new ArrayList<>();
contacts.add(new Contact("Michael"),null);
contacts.add(new Contact("Michael"),Arrays.asList(new PhoneNumber("+23470390989"));
and so on...
Upvotes: 1
Reputation: 2197
Something like this
public class Contact {
private String displayName = null;
private List<PhoneNumber> phoneNumbers = null;
public Contact() {}
public Contact(String displayName, List<PhoneNumber> phoneNumbers) { this.displayName = displayName; this.phoneNumbers = phoneNumbers; }
public String getDisplayName() { return displayName; }
public void setDisplayName(String displayName) { this.displayName = displayName; }
public List<PhoneNumber> getPhoneNumbers() { return phoneNumbers; }
public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) { this.phoneNUmbers = phoneNumbers; }
}
public class PhoneNumber {
private String value = null;
public PhoneNumber() {}
public PhoneNumber(String value) { this.value = value; }
public String getValue() { return value; }
public void setValue(String value) { this.value = value }
}
public Class ContactsTest {
public static void main(String[] args) {
List<Contact> contacts = new ArrayList<>();
Contact contact = new Contact("Michael", null);
contacts.add(contact);
List<PhoneNumber> phoneNumbers = new ArrayList<>();
phoneNumbers.add(new PhoneNumber("+23470390989"));
contacts.add(new Contact("Efe", phoneNumbers);
Gson gson = new GsonBuilder().create();
System.out.println(gson.toJson(contacts));
}
}
Upvotes: 1