简体   繁体   English

创建复杂的Json结构

[英]Creating Complex Json Structure

my challenge is that I find it difficult and tricky to Format string to a json data such as this: 我的挑战是我很难将字符串格式化为json数据,例如:

{
    "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. 而我正在尝试格式化的字符串来自Getcontact类(它从电话中获取联系人列表),希望很多人都熟悉从移动设备获取联系人的方法。

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. 谢谢你们。

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

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

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 . 下面是我的代码的最终外观,这些代码用于从电话中获取联系人并以REQUESTED JSON FORMAT形式返回。 So the main setups happen in CODE SECTION 2 . 因此,主要设置发生在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*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM