Getting JSON Exception no value for?

核能气质少年 提交于 2019-12-10 11:34:39

问题


I am trying to handle the json response, that is like

{"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"}

but getting org.json.JSONException: no value for exception

My code is this.

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:


Try this code:

if (jsonStr != null) {
  try {

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

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

Your JSON string doesn't have an array in it, so the object that you will be getting from the first JSONObject is already where your information lives.



来源:https://stackoverflow.com/questions/30127839/getting-json-exception-no-value-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!