【问题标题】:How can i fetch the following data?如何获取以下数据?
【发布时间】:2014-03-15 07:05:02
【问题描述】:

我想从此代码中提取男性对象,以便我的输出显示所有性别 里面有男的。

  {
   "contacts": [
      {
            "id": "c200",
            "name": "Ravi Tamada",
            "email": "ravi@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c201",
            "name": "Johnny Depp",
            "email": "johnny_depp@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
           }
    },

这是我试图获取的 json 代码

`if (jsonStr != null) { 尝试 { JSONObject jsonObj = new JSONObject(jsonStr);

                contacts = jsonObj.getJSONArray(TAG_CONTACTS);


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


                    if(TAG_GENDER == male ){


                    String name = c.getString(TAG_GENDER);


                    HashMap<String, String> contact = new HashMap<String, String>();

                    contact.put(TAG_GENDER, name);

                    contactList.add(contact);
                }}  }
            catch (JSONException e) {
                e.printStackTrace();
            }
            }
         else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }`

【问题讨论】:

  • 你试过的解析代码在哪里?
  • 什么是if(TAG_GENDER == male ){TAG_GENDERmale 是什么?
  • 私有静态字符串 url = "api.androidhive.info/contacts";这就是我在代码中声明的内容: private static final String TAG_CONTACTS = "contacts";私有静态最终字符串 TAG_NAME = "名称";私有静态最终字符串 TAG_GENDER = "性别";
  • @user3422497 TAG_GENDER 是什么?

标签: android json


【解决方案1】:

这里 "{" 显示 json 对象,"[" 显示 json 数组,所以我们将创建 json 对象,然后像这样创建 jsonArray..

JSONObject jsonObj = new JSONObject(JsonObjectKey);

            // if( jsonObj!=null)
            // {
            JSONArray list = jsonObj.getJSONArray("contacts");// here your json array aname
            // if(list!=null)
            for (int i = 0; i < list.length(); i++) {
//here you will fetch your all values that in your array..
}

【讨论】:

    【解决方案2】:

    我正在修改你的 for 循环..

              for (int i = 0; i < contacts.length(); i++)
               {
               JSONObject c = contacts.getJSONObject(i);
    
                if((JSONObject)contacts.get(i)).get("gender").toString().equals("male") ){
    
    
                String name = (JSONObject)contacts.get(i)).get("name").toString();
    
    
                HashMap<String, String> contact = new HashMap<String, String>();
    
                contact.put(TAG_GENDER, name);
    
                contactList.add(contact);
            }}
    

    【讨论】:

      【解决方案3】:

      你需要做一些修正。

      1. 在 for 循环之外声明您的哈希映射
      2. 而不是在 if 循环中使用 '==' 使用 .equals

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-02
        • 2021-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-26
        • 1970-01-01
        相关资源
        最近更新 更多