我有這種格式的代碼。任何人都可以幫助我如何讀取數組?我需要讀取聯繫人數組。在java/Android中解析JSON
{
"success": true,
"contacts": [
{
"username": "ceema",
"profile": {
"name": "Ceema S",
"email": "[email protected]",
"address": "No 143, gangaeyam, Maikkad P O, Kerala",
"phone": {
"mobile": "3333",
"home": "2222",
"office": "6666"
}
}
},
{
"username": "hari",
"profile": {
"name": "Harikrishnan V S",
"email": "[email protected]",
"address": "No 143, harihome, Maikkad P O, Kerala",
"phone": {
"mobile": "3333",
"home": "2222",
"office": "6666"
}
}
},
{
"username": "pranav",
"profile": {
"name": "Pranav Mohan",
"email": "[email protected]",
"address": "No 143, harihome, Maikkad P O, Kerala",
"phone": {
"mobile": "3333",
"home": "2222",
"office": "6666"
}
}
}
]
}
我嘗試這樣:
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
//String id = c.getString(TAG_NAME);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
//String gender = c.getString(TAG_GENDER);
// Phone node is JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
這jsonstr是從網站JSON響應。 我需要閱讀嵌套的聯繫人,並將其填充到
什麼是錯誤? – Rustam