0

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.

3
  • whats confusing can you explain bit more Commented Jul 3, 2018 at 17:19
  • I could format those strings into arrays, but I'm not sure how I can then make those array a group of arrays in an object....just the way I put it up there(The result I want), looking at what I have done so far, I can take the contact names which is quite easy, as for the contacts number(Then i will have to store it like groups of object in an array and that's a bit challenging to me.) Commented Jul 3, 2018 at 17:30
  • You could use GSon then call Gson gson = new GsonBuilder().create(); gson.toJson(topLevelObject);. You have to create a new array for each break; Commented Jul 3, 2018 at 17:35

3 Answers 3

1

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));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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...

Comments

0

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*/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.