我是json解析的新手,很忙。我必須解析以下內容: -在Android中解析JSONArray
[
{
"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,
},
}
]
我需要從上面獲取「login」和「userid」。下面是我寫的代碼: -
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");
}.
問題是我的代碼以異常結束。 請幫忙!!!
您忘記解析' 「用戶」:{'在其下你ahve用戶名和ID – Raghunandan