获取JSON异常没有价值吗? [英] Getting JSON Exception no value for?

查看:151
本文介绍了获取JSON异常没有价值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但得到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();
            }

推荐答案

尝试以下代码:

if (jsonStr != null) {
  try {

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

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

您的JSON字符串中没有数组,因此从第一个JSONObject获取的对象已经是您的信息所在的地方.

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.

这篇关于获取JSON异常没有价值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆