获取JSON异常没有价值?

时间:2015-05-08 15:45:34

标签: json android-json

我正在尝试处理json响应,就像

{"Status":true,"UserId":111,"FirstName":"dev","LastName":"dev","Gender":-1,"BirthdayDate":"0000-00-00","Phone":"","ProfilePicture":"","ProfilePicture60px":"","ProfilePicture120px":"","CountryId":-1,"Email":"droidwithmxxmail.com","Password":"******123","RegisterDate":"2015-05-08 20:08:07","SessionId":"fce248fe6499b7a9338a1b64554509eb77841"}

但获得org.json.JSONException: no value for exception

我的代码就是这个。

if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

            allres =jsonObj.getJSONArray(jsonStr);

                for (int i = 0; i < allres.length(); i++) {
                    JSONObject c = allres.getJSONObject(i);
                   userId = c.getString("UserId");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

1 个答案:

答案 0 :(得分:0)

试试这段代码:

if (jsonStr != null) {
  try {

    JSONObject jsonObj = new JSONObject(jsonStr);
   userId = jsonObj.getString("UserId");

  } catch (JSONException e) {
      e.printStackTrace();
}

您的JSON字符串中没有数组,因此您从第一个JSONObject获取的对象已经存在于您的信息所在的位置。