使用Json获取响应,当某个对象没有出现时,应用程序崩溃?

时间:2016-09-24 07:02:32

标签: android json

我正在使用我的响应创建一个模型,使用JSOn创建模型。作为回应,一些参数来了一段时间,很少有一些时间没来,我的代码给出了。  1.回应

{
"result": {
"success": true,
"data": [
  {
    "uid": "xxxxxxx",
    "total_activities": "19",
    "points": 105,
    "guesstimated_reach": "39",
    "rank": 1,
    "bonus_points": {
      "details": [],
      "total_bonus_points": 0
    },
    "user_details": {
      "first_name": "Pauras More",
      "last_name": "!",
      "city": "Mumbai",
      "email": "xxxxxxx.com",
      "snetwork_influence": {
        "fb": {
          "friends": 11
        },
        "ln": {
          "connections": 116
        },
        "tw": {
          "friends": 50,
          "followers": 3,
          "listed": 18,
          "favorites": 224,
          "statuses": 1380
        }
      },
      "saccounts": {
        "fb": {
          "id": "xxxxxxx",
          "first_name": "xxxxxxx",
          "last_name": "xxxxxxx",
          "extra_data": {
            "timeline_album_id": "xxxxxxx"
          }
        },
        "ln": {
          "id": "xxxxxxx",
          "first_name": "xxxxxxx",
          "last_name": "xxxxxxx"
        },
        "tw": {
          "id": "xxxxxxx",
          "first_name": "xxxxxxx",
          "last_name": ""
        }
      },
      "pic": "xxxxxxx"
    }
  },
  {
    "uid": "xxxxxxx",
    "total_activities": "21",
    "points": 72,
    "guesstimated_reach": "100",
    "rank": 2,
    "bonus_points": {
      "details": [],
      "total_bonus_points": 0
    },
    "user_details": {
      "first_name": "xxxxxxx",
      "last_name": "T",
      "city": "xxxxxxx",
      "email": "xxxxxxx.com",
      "snetwork_influence": {
        "ln": {
          "connections": 19
        },
        "tw": {
          "friends": 0,
          "followers": 0,
          "listed": 0,
          "favorites": 0,
          "statuses": 0
        },
        "fb": {
          "friends": 527
        }
      },
      "saccounts": {
        "ln": {
          "id": "xxxxxxx",
          "first_name": "xxxxxxx",
          "last_name": "xxxxxxx"
        },
        "tw": {
          "id": "xxxxxxx",
          "first_name": "xxxxxxx",
          "last_name": ""
        },
        "ig": {
          "id": "xxxxxxx",
          "first_name": "xxxxxxx",
          "last_name": "",
          "username": "xxxxxxx"
        },
        "fb": {
          "id": "xxxxxxx",
          "first_name": "xxxxxxx",
          "last_name": "xxxxxxx",
          "extra_data": {
            "timeline_album_id": "xxxxxxx"
          }
        }
      },
      "pic": "xxxxxxx.jpg"
    }
  }
  ]
}

}

  • 我的Model类是,

    public class SAccountConnectModel {
     /* Social Account name*/
    public String SAccount_facebook = "false";
    public String SAccount_linkedin = "false";
    public String SAccount_twitter = "false";
    public String SAccount_instagram = "false";
    public String SAccount_youtube = "false";
    
    public String getSAccount_facebook() {
    return SAccount_facebook;
    }
    
    public void setSAccount_facebook(String SAccount_facebook) {
    this.SAccount_facebook = SAccount_facebook;
    }
    
    public String getSAccount_linkedin() {
    return SAccount_linkedin;
    }
    
    public void setSAccount_linkedin(String SAccount_linkedin) {
    this.SAccount_linkedin = SAccount_linkedin;
    }
    
    public String getSAccount_twitter() {
    return SAccount_twitter;
    }
    
    public void setSAccount_twitter(String SAccount_twitter) {
    this.SAccount_twitter = SAccount_twitter;
    }
    
    public String getSAccount_instagram() {
    return SAccount_instagram;
    }
    
    public void setSAccount_instagram(String SAccount_instagram) {
    this.SAccount_instagram = SAccount_instagram;
    }
    
    public String getSAccount_youtube() {
    return SAccount_youtube;
    }
    
    public void setSAccount_youtube(String SAccount_youtube) {
    this.SAccount_youtube = SAccount_youtube;
    }
    }
    
  • 代码是

    //Decleration
    private SAccountConnectModel mSAccountConnectModel = new      SAccountConnectModel();
    private ArrayList<SAccountConnectModel> mSAccountConnectModelArrayList = new ArrayList<SAccountConnectModel>();
    
    //Code
    /*Leader Board Network Connectivity Check and Display only connected network*/
                JSONObject mainObject = null, resultObject = null, userObject = null, userDetailObject = null, sAccountObject = null,
                        fbNetwokObject = null, twNetworkObject = null, igNetworkObject = null, lnNetworkObject = null, ytNetworkObject = null;
                try {
                    mainObject = new JSONObject(response);
                    resultObject = mainObject.getJSONObject("result");
                    if (mainObject != null) {
                        JSONArray dataJsonArray = resultObject.getJSONArray("data");
                        if (dataJsonArray != null) {
                            for (int i = 0; i < dataJsonArray.length(); i++) {
                                Logger.e(TAG, "My Response Data : " + dataJsonArray.get(i));
                                userObject = (JSONObject) dataJsonArray.get(i);
                                if (userObject != null) {
                                    userDetailObject = (JSONObject) userObject.get("user_details");
                                    Logger.e(TAG, "My Response User Details Data : " + userDetailObject);
                                    if (userDetailObject != null) {
                                        sAccountObject = userDetailObject.getJSONObject("saccounts");
                                        Logger.e(TAG, "My Response Social Account Data : " + sAccountObject);
                                        if (sAccountObject != null) {
                                            try {
                                                fbNetwokObject = sAccountObject.getJSONObject("fb");
                                                twNetworkObject = sAccountObject.getJSONObject("tw");
                                                igNetworkObject = sAccountObject.getJSONObject("ig");
                                                lnNetworkObject = sAccountObject.getJSONObject("ln");
                                                ytNetworkObject = sAccountObject.getJSONObject("yt");
                                                if (fbNetwokObject != null)
                                                    mSAccountConnectModel.setSAccount_facebook("true");
                                                else
                                                    mSAccountConnectModel.setSAccount_facebook("false");
    
                                            } catch (JSONException e) {
    
                                            }
                                            mSAccountConnectModelArrayList.add(i,mSAccountConnectModel);
                                        }
                                        /*if (sAccountObject != null) {
                                            *//*Check Facebook Connection Available or not*//*
                                            if (sAccountObject.getJSONObject("fb") != null)
                                                mSAccountConnectModel.setSAccount_facebook("true");
                                            else
                                                mSAccountConnectModel.setSAccount_facebook("false");
                                            *//*Check LinkedIn Connection Available or not*//*
                                            if (sAccountObject.getJSONObject("ln") != null)
                                                mSAccountConnectModel.setSAccount_linkedin("true");
                                            else
                                                mSAccountConnectModel.setSAccount_linkedin("false");
                                            *//*Check Instagram Connection Available or not*//*
                                            if (sAccountObject.getJSONObject("ig") != null)
                                                mSAccountConnectModel.setSAccount_instagram("true");
                                            else
                                                mSAccountConnectModel.setSAccount_instagram("false");
                                            *//*Check YouTube Connection Available or not*//*
                                            if (sAccountObject.getJSONObject("yt") != null)
                                                mSAccountConnectModel.setSAccount_youtube("true");
                                            else
                                                mSAccountConnectModel.setSAccount_youtube("false");
                                            if (sAccountObject.getJSONObject("tw") != null)
                                                mSAccountConnectModel.setSAccount_twitter("true");
                                            else
                                                mSAccountConnectModel.setSAccount_twitter("false");
                                        }*/
                                    }
                                }
    
                                Logger.e(TAG, "Model Size is : " + mSAccountConnectModelArrayList.size());
                            }
                        }
                        Logger.e(TAG, "Model Size is : " + mSAccountConnectModelArrayList.size());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
    

2 个答案:

答案 0 :(得分:0)

首先使用下面的行检查特定json对象的键。

例如:

if(sAccountObject.has("fb")){

 fbNetwokObject = sAccountObject.getJSONObject("fb");
}

如果这不是您的问题,请在此处添加崩溃报告。

答案 1 :(得分:0)

为什么不用opt替换get。

实施例 更换 twNetworkObject = sAccountObject.getJSONObject("tw"); twNetworkObject = sAccountObject.optJSONObject("tw");

read this for whole documentation