我的挑战是我很难将字符串格式化为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"
}
]
}
]
}
我正在尝试格式化的字符串来自Getcontact类(它从电话中获取联系人列表),希望很多人都熟悉从移动设备获取联系人的方法。
尝试
到目前为止,我尝试过的是:
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);
这
ArrayList<ContactsStructure> contacts = new ArrayList<ContactsStructure>();
contacts.add(contactsStructure);
但是我不是很正确!这令人困惑...
任何帮助都会很好。 谢谢大家。
答案 0 :(得分:1)
类似这样的东西
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));
}
}
答案 1 :(得分:1)
如果我正确理解了您要问的内容,则您的课程应如下所示:
class Contact {
String displayName;
ArrayList<PhoneNumber> phoneNumbers;
// constructors and getter/setters
}
class PhoneNumber {
String value;
// constructor and getter/setters
}
现在,您需要创建一个联系人数组。
ArrayList<Contact> contacts = new ArrayList<>();
contacts.add(new Contact("Michael"),null);
contacts.add(new Contact("Michael"),Arrays.asList(new PhoneNumber("+23470390989"));
以此类推...
答案 2 :(得分:0)
为了清楚起见,我只需要坚持这一点。 竖起大拇指所有回复
/*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);
所以实际发生的是由于我从电话簿中获取联系人的方式,使得使用该代码变得容易。以下是我的代码的最终外观,这些代码可用于从电话中获取联系人并以 REQUESTED JSON FORMAT 返回。因此,主要设置发生在代码第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*/