2014-04-08 114 views
0

我是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"); 
    }. 

問題是我的代碼以異常結束。 請幫忙!!!

+0

您忘記解析' 「用戶」:{'在其下你ahve用戶名和ID – Raghunandan

回答

1

usernameuseriduser內的JSONObject分析用戶的JSONObject,然後獲取用戶名和用戶ID的字符串。

DO這樣得到usernameuserid

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); 
    } 
+0

謝謝@Nambi Narayanan ..奇妙的作品! ...但我後悔我不能「投票」你的答案,因爲我沒有足夠的聲譽:(。Neways感謝噸 – saugatchetry

+0

考慮到查看Gson語法分析器,它是相當了不起的。 – Joris

0

解析:「用戶」

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

jresult = json.getJSONArray("user"); 
for (int i = 0; i < jresult .length(); i++) 
{ 
JSONObject obj = jresult .getJSONObject(i); 
String username=obj.getString("username"); 
int userid=obj.getInt("userid"); 
} 
0
if(result != null) 
      { 
       try 
       { 
        JSONObject jobj = result.getJSONObject("result"); 

        String status = jobj.getString("status"); 

        if(status.equals("true")) 
        { 
         JSONArray array = jobj.getJSONArray("user"); 

         for(int x = 0; x < array.length(); x++) 
         { 
          HashMap<String, String> map = new HashMap<String, String>(); 

          map.put("username", array.getJSONObject(x).getString("username")); 

          map.put("userid", array.getJSONObject(x).getString("userid")); 



          list.add(map); 
         } 

         CalendarAdapter adapter = new CalendarAdapter(Calendar.this, list); 

         list_of_calendar.setAdapter(adapter); 
        } 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
0

試試這個代碼::)

JSONArray jsonObj = new JSONArray(response); 
       for(int i=0 ; i<jsonObj.length(); i++) 
       {          
        JSONObject json_Data = jsonObj.getJSONObject(i); 
        String firstname = json_Data.getString("firstname"); 
        String lastname = json_Data.getString("lastname"); 
        String designation = json_Data.getString("designation"); 

        JSONArray jsons1 = json_Data.getJSONArray("user"); 
        for (int j = 0; j < jsons1.length(); j++) { 

         JSONObject jsonss = jsons1.getJSONObject(j); 
         String username = jsonss.getString("username"); 
          String userid = jsonss.getString("userid"); 
        } 

       } 
       }catch (JSONException e) { 
        Log.d("Failure","Dude I have failed"); 
       }