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,
},
}
]
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");
}.
The problem is that my code ends up with exception. Please help !!!
"user": {under which you ahve username and id