解析布尔JSON值总是给我假

时间:2016-04-21 07:56:55

标签: android json

我得到以下JSON:

    {
    "ID": "479",
    "id": "479",
    "name": "Nishant Kango",
    "last_logged": "1461225061",
    "current_status": "icon-online prople-online",
    "room_id": "d18f655c3fce66ca401d5f38b48c89af",
    "current_user": false,
    "lastmessage": [],
    "userInfo": {
        "id": "479",
        "birthday": "1989-11-21",
        "comment_privacy": "everyone",
        "confirm_followers": "1",
        "current_city": "Chandigarh, Chandigarh",
        "follow_privacy": "everyone",
        "gender": "male",
        "hometown": null,
        "message_privacy": "everyone",
        "timeline_post_privacy": "a:1:{i:0;s:1:\"1\";}",
        "feed_section": "1",
        "post_privacy": "a:1:{i:0;s:1:\"5\";}",
        "current_city_id": "1433",
        "userSkills": ", Testing ",
        "birth": {
            "date": "21",
            "month": "11",
            "year": "1989"
        },
        "about": "asdasdasdas sad asd asd asd as",
        "active": "1",
        "avatar_id": "477",
        "cover_id": "528",
        "cover_position": "465",
        "email": "nishantkango@yahoo.co.in",
        "email_verification_key": null,
        "email_verified": "0",
        "language": null,
        "last_logged": "1461225061",
        "name": "Nishant Kango",
        "time": "0",
        "timestamp": "2016-04-21 13:19:40",
        "timezone": null,
        "type": "user",
        "username": "nishant",
        "verified": false,
        "user_id": "479",
        "first_name": "Nishant",
        "middle_name": null,
        "last_name": "Kango",
        "dob": "1989-11-21",
        "mobile": "8699022278",
        "title": null,
        "marital_status": "single",
        "childrens": "0",
        "hobbies": "Travelling",
        "interests": "Technology",
        "feed_back": null,
        "status": "approved",
        "profile_pic": null,
        "date_created": "2014-10-29 00:00:00",
        "date_modified": "2016-03-31 19:07:56",
        "activation_code": null,
        "social_login_type": "facebook",
        "facebook_id": "10205077190793111",
        "linked_in_id": null,
        "google_id": null,
        "employment_status": null,
        "is_deleted": "0",
        "website": "http://www.nishantkango.com",
        "reference_institute": "",
        "industry": "0",
        "proffesion_type": "",
        "url": "http://192.168.2.250:82/user/timeline/e6b34e9a0311a7f829e09d2d7c4b313e",
        "cover": {
            "id": "528",
            "active": "1",
            "album_id": "0",
            "extension": "jpg",
            "name": "mentordirectorybgimagesuggestions6.jpg",
            "post_id": "0",
            "temp": "0",
            "timeline_id": "0",
            "type": "photo",
            "url": "common/marvel/photos/2016/02/hqUDT_528_f4be00279ee2e0a53eafdaa94a151e2c",
            "complete_url": "http://192.168.2.250:82/common/marvel/photos/2016/02/hqUDT_528_f4be00279ee2e0a53eafdaa94a151e2c.jpg",
            "post_url": "/index.php?tab1=story&id=0"
        },
        "actual_cover_url": "http://192.168.2.250:82/common/marvel/photos/2016/02/hqUDT_528_f4be00279ee2e0a53eafdaa94a151e2c.jpg",
        "cover_url": "http://192.168.2.250:82/common/marvel/photos/2016/02/hqUDT_528_f4be00279ee2e0a53eafdaa94a151e2c_cover.jpg",
        "avatar": {
            "id": "477",
            "active": "1",
            "album_id": "0",
            "extension": "jpg",
            "name": "10390233_10205765787487598_5674312231016706405_n.jpg",
            "post_id": "0",
            "temp": "0",
            "timeline_id": "0",
            "type": "photo",
            "url": "common/marvel/photos/2016/01/yS1xw_477_74071a673307ca7459bcf75fbd024e09",
            "complete_url": "http://192.168.2.250:82/common/marvel/photos/2016/01/yS1xw_477_74071a673307ca7459bcf75fbd024e09.jpg",
            "post_url": "/index.php?tab1=story&id=0"
        },
        "thumbnail_url": "http://192.168.2.250:82/common/marvel/images/default-male-avatar.png",
        "avatar_url": "http://192.168.2.250:82/common/marvel/images/default-male-avatar.png",
        "online": true
    }
}

我必须使用userInfo对象中的在线密钥检查用户的在线/离线状态。我使用以下代码:

JSONObject userInfo = object.getJSONObject("userInfo");
 Boolean online = userInfo.getBoolean("online");
Log.e("Online",online);

当我尝试使用此声明时:

Log.e("online1", (String) userInfo.get("online")); 

它给出了java.lang.ClassCastException:java.lang.Boolean无法强制转换为java.lang.String的错误。

所以可以肯定userInfo.get(“online”)给我一些布尔值。所以当我使用它时:

Log.e("online1", String.valueOf(userInfo.getBoolean("online")));

它让我总是假的,但我知道在某些情况下我从服务器获得了真正的价值。请帮助我解决问题

5 个答案:

答案 0 :(得分:1)

您确定get()函数返回boolean。我不确定使用Android的JSON库,但我使用的将返回值作为字符串,然后您需要使用Boolean.parseBoolean(value)进行解析。 希望有所帮助。

答案 1 :(得分:0)

你可以尝试

  

Boolean online = Boolean.parseBoolean(userInfo.getString(" online"))

这对我有用。

答案 2 :(得分:0)

 Log.e("Online", online.toString());

只需要String。

Log类型中的方法e(String,String)不适用于参数(String,boolean)。

答案 3 :(得分:0)

你试试boolean online = userInfo.getBoolean(“online”);而不是布尔在线= userInfo.getBoolean(“在线”);

答案 4 :(得分:0)

我建议使用opt版本而不是get版本。

主要区别在于opt不会抛出JSONException并停止解析。

请尝试将所有getXXX()替换为optXXX()

另一个好处是,使用opt,您可以指定后备值。