gpt4 book ai didi

java - 在java/Android中解析JSON

转载 作者:行者123 更新时间:2023-12-01 11:09:40 27 4
gpt4 key购买 nike

我有这种格式的代码。谁能帮助我如何读取数组?我需要读取联系人数组。

 {
"success": true,
"contacts": [
{
"username": "ceema",
"profile": {
"name": "Ceema S",
"email": "ceemas@gmail.com",
"address": "No 143, gangaeyam, Maikkad P O, Kerala",
"phone": {
"mobile": "3333",
"home": "2222",
"office": "6666"
}
}
},
{
"username": "hari",
"profile": {
"name": "Harikrishnan V S",
"email": "harikriii@gmail.com",
"address": "No 143, harihome, Maikkad P O, Kerala",
"phone": {
"mobile": "3333",
"home": "2222",
"office": "6666"
}
}
},
{
"username": "pranav",
"profile": {
"name": "Pranav Mohan",
"email": "pranavmohan@gmail.com",
"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响应。我需要读取嵌套的联系人并将其填充到

最佳答案

This jsonstr is json response from the site. I need to read the nested contacts and populate it to

你几乎猜对了。您正在寻找的对象在个人资料内

JSONObject c = contacts.getJSONObject(i);
JSONObject profile = c.getJSONObject("profile");

并使用profile而不是c来查看姓名、电话等

关于java - 在java/Android中解析JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32520959/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com