【问题标题】:Get json array values android获取json数组值android
【发布时间】:2014-11-25 03:23:32
【问题描述】:

我有一个关于如何解析 json 文件的问题。我的json结构是这样的:

{
    "contacts": [
        {
                "id": "c200",
                "name": "Ravi Tamada",
                "email": "ravi@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "services": [
                   "laundry",
                   "wifi",
                   "tv",
                   "swimming pool",
                   "bar"
                 ],
                "phone": [
                    910000000000,
                    00000000,
                    000000
                ]
        },
        {
                "id": "c201",
                "name": "Johnny Depp",
                "email": "johnny_depp@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "services": [
                   "laundry",
                   "wifi",
                   "tv",
                   "swimming pool",
                   "bar"
                 ],
                "phone": [
                    0000000000,
                    00000000,
                    00000000
                ]
        }
  ]

如何获取电话价值和服务价值?

phones = jsonObj.getJSONArray(TAG_PHONE);
for (int x = 0; x < phones.length(); x++) {

}

因为例如获取 ID 我没有遇到问题:

for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        String id = c.getString(TAG_ID);
// tmp hashmap for single contact
                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        contact.put(TAG_ID, id);
contactList.add(contact);

非常感谢

【问题讨论】:

标签: android json


【解决方案1】:

PhoneServices 是 JSONArray 对象,所以当你执行 get 函数时,你应该使用 .getJSONArray()

例如:

JSONArray phoneArray = c.getJSONArray("phone");
for(int i=0;i<phoneArray.length();i++){
    JSONObject json_phone_data = phoneArray.getJSONObject(i);
    String phone_data = phoneArray.getString(i);
    // Do something with phone data
}

JSONArray servicesArray = c.getJSONArray("services");
for(int i=0;i<servicesArray.length();i++){
    JSONObject json_services_data = servicesArray.getJSONObject(i);
    String services_data = servicesArray.getString(i);
    // Do something with services data
}

documentation

【讨论】:

  • 非常感谢,请问如何获取json_data i 元素的字符串值?谢谢:)
  • 如果你想要字符串,你可以简单地使用String json_phone_data = phoneArray.getString(i);
  • 在适配器列表中我该怎么做? TextView phone = (TextView)vi.findViewById(R.id.telefono_alojamiento); HashMap 联系人 = new HashMap();联系人 = data.get(位置); phone.setText(song.get(MyFragment.TAG_PHONE)));
  • 没有看到你的完整代码,我不能肯定地说。但这并不是您使用 HashMaps 的真正方式。通常你会做类似contact.put("key", value); 的事情。此外,只要song.get(MyFragment.TAG_PHONE) 是一个可以隐式转换为字符串的值,您的 setText 就可能是正确的。通常,人们喜欢尽可能使用.getString()。另外,如果这为您解决了问题,请投票并将其作为解决方案。
【解决方案2】:

Services 和 Phone 是 Contacts 的内部 JSONArray。因此,您可以从联系人 JSONObject 中使用他们的密钥检索相应的对象并循环

for (int i = 0; i < contacts.length(); i++) {
    JSONObject c = contacts.getJSONObject(i);
    JSONArray phone = c.optJSONArray("phone")
    if (phone != null) {
       for (int x = 0; x < phones.length(); x++) {
            Log.i("PHONE", "phone at #" + x + " " + phone.optInt(x));
        }   
    }

    JSONArray services = c.optJSONArray("services"); 
    if (services != null) {
      for (int j = 0; j < services.length(); j++) {
            Log.i("SERVICE", "service at #" + j + " " + services.optString(j));
      } 
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多