在Android的解析JSONArray [英] Parsing JSONArray in Android

查看:180
本文介绍了在Android的解析JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的JSON解析和我赶上了大的时间。我要解析如下: -

I am new to json parsing and am caught up big time. I have to parse the following:-

[
  {
    "firstname": abc,
    "lastname": xyp,
    "designation" : executive,
    "user": {
      "username": "xypabc",
      "userid": 4003,
      },
  },

  {
    "firstname": pqr,
    "lastname": vbn,
    "designation" : security,
    "user": {
      "username": "vbnpqr",
      "userid": 11231,
      },
  },    


  {
    "firstname": ghk,
    "lastname": lkj,
    "designation" : manager,
    "user": {
      "username": "lkjghk",
      "userid": 774,
      },
  }
]

我要取的登录,并从上面的用户id。下面是code,我写道: -

I need to fetch the "login" and "userid" from above. Below is the code which I wrote :-

try {
    JSONArray jsonObj = new JSONArray(response);
    for(int i=0 ; i<jsonObj.length(); i++)
    {                                       
        JSONObject json_Data = jsonObj.getJSONObject(i);
        String userName = json_Data.getString("username");
        String userId = json_Data.getString("userid");
        Log.d("Factors","UserName :- "+userName+" ID :- "+userId);
    }
    }catch (JSONException e) {
        Log.d("Failure","Dude I have failed");
    }.

问题是,我的code与异常结束。
请帮助!

The problem is that my code ends up with exception. Please help !!!

推荐答案

用户名用户ID 是在用户的JSONObject解析用户的JSONObject,然后得到的用户名和用户ID的字符串。

username and userid is within the user JSONObject parse the user JSONObject and then get the string of the username and userid.

DO这样获得用户名用户ID

for(int i=0 ; i<jsonObj.length(); i++)
    {                                       
        JSONObject json_Data = jsonObj.getJSONObject(i);
        String userName = json_Data.getJSONObject("user").getString("username");
        String userId = json_Data.getJSONObject("user").getString("userid");
        Log.d("Factors","UserName :- "+userName+" ID :- "+userId);
    }

这篇关于在Android的解析JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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