无法从jsonObject获取

时间:2017-02-02 17:03:32

标签: java android json

我有这个JSON字符串。

{
    "drtest": {
        "password":"dr123",
        "presence": {
            "lastSeen":1485992132269,
            "status":"offline"
         },
         "role":"Doctor",
         "speciality":"Physiotherapist",
         "token":"f80ve0sY6Ak:APA91bGLPlmy7b_iyqtW9ioG11TOx3xTB9BJgb1vvBK4YRo-28DyXWydg476TvjzaxyNB3kTTMn0BycEDm9UJHAPeoWzV-vFrwN46hg-GKSI6DH1s9zrH8h834c2whEdr12XqWN-4jrs"
     },
    "patient": {
         "password":"pat123",
         "presence": {
             "lastSeen":1486046501150,
             "status":"online"
         },
         "role":"Patient",
         "token":"f80ve0sY6Ak:APA91bGLPlmy7b_iyqtW9ioG11TOx3xTB9BJgb1vvBK4YRo-28DyXWydg476TvjzaxyNB3kTTMn0BycEDm9UJHAPeoWzV-vFrwN46hg-GKSI6DH1s9zrH8h834c2whEdr12XqWN-4jrs"
    }
}

首先,我一直在跟踪医生角色,但现在我必须跟踪专业。但是,我正在尝试正常工作的代码,但它没有按预期工作。

正如您所看到的,只有一个实体拥有专业数据而其他实体不包含专业领域。我如何解析专业数据(如果存在)并忽略JSON中是否没有专业数据?

我正在使用此代码:

    try {
        contactList.clear();
        JSONObject obj = new JSONObject(s);
        Iterator i = obj.keys();
        String key = "";

        while (i.hasNext()) {
            key = i.next().toString();
            JSONObject singleUser = (JSONObject) obj.get(key);
            String role = singleUser.get("role").toString();

            JSONObject singleUser2 = (JSONObject) obj.get(key);

            String tpesu= singleUser2.getString("speciality");

            // no success here? need help want this value...
            Log.e("specialittyyyyyyy",tpesu+"");

            JSONObject presence = singleUser.getJSONObject("presence");
            String status = presence.getString("status");

            if (!key.equals(UserDetails.username)) {
                if (role.equals("Doctor")) {
                    HashMap<String, String> docs = new HashMap<>();
                    docs.put("name", key);
                    docs.put("status", status);
                    Log.e("status ", status + "");
                    contactList.add(docs);
                }
            }
            totalUsers++;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

3 个答案:

答案 0 :(得分:3)

将singleUser包含在内以避免格式或空指针异常

if(singleUser2.has("speciality")) {
    tpesu = singleUser2.getString("speciality");
}

全局声明字符串tpesu。

答案 1 :(得分:1)

您可以使用“has”功能。即if(singleUser2.has(“special”))...

答案 2 :(得分:0)

您的singleUser&amp; singleUser2 json对象是相同的(drtest),因为您通过使用相同的key值引用它们来检索它们。而是通过下一个可用密钥(下例中的singleUser2)检索singleUser2key=...

JSONObject obj = new JSONObject("dd");
Iterator i = obj.keys();
String key = "";
while (i.hasNext()) {
    key = i.next().toString();
    String singleUser2key = i.next().toString();

    JSONObject singleUser = (JSONObject) obj.get(key);
    String role = singleUser.get("role").toString();

    JSONObject singleUser2 = (JSONObject) obj.get(singleUser2key);

    String tpesu= singleUser2.getString("speciality");
    // no success here ??? need help want this value...
    Log.e("specialittyyyyyyy",tpesu+"");