2017-08-28 87 views
0

我有這樣JSON數據時,它是具有多個it.how jsonArrays解析這種類型的數據?如何分析它有多個jsonArrays在Android的JSON數據?

{ 
     "result": 
     [{ 
     "site_name":"comenius", 
     "ws_url":"https://comenius-api.sabacloud.com/v1/", 
     "base_url":"https://comenius.sabacloud.com/", 
     "logo_url":"", 
     "Title":"", 
     "menu_items":[ 
       {"item": 
        [{"id":"home1","label":"Home" }] 
        }, 
       {"item":  
        [{"id":"enrol1","label":"Enrollment" }] 
       }, 
       {"item": 
        [{"id":"transcripts1","label":"Completed Courses"}] 
       }, 
       {"item": 
        [{"id":"goals1","label":"Goals"}] 
       }, 
       {"item": 
        [{"id":"rprojects1","label":"Reference Projects"}] 
       }, 
       {"item": 
         [{"id":"iwh1","label":"Internal Work History"}] 
       }, 
       {"item": 
        [{"id":"ewh1","label":"EXternal Work History"}] 
       } 
       ] 
     },{.....} 
] 
} 

我需要分析的數據,並獲得ID值,標籤,我寫一些代碼來分析數據,但它沒有工作。

JSONObject subObj = new JSONObject(data2); 
    JSONObject innerObj1 = subObj.getJSONObject("result"); 
    JSONObject subArrayObj = innerObj1.getJSONObject("menu_items"); 
    for(int j =0;j < subArrayObj.length();j++) { 
    JSONObject innsersubObj = subArrayObj.getJSONObject("item"); 
    String id = innsersubObj.getString("id"); 
    String label = innsersubObj.getString("label"); 
    Log.e("id",id); 
    Log.e("label",label); 
    } 

如何解析數據中需要更改的任何代碼?

回答

1

你必須使用的JSONObject和JSONArray是不同的對象,你必須使用正確的類:

JSONArray resultArray = subObj.getJSONArray("result"); 
JSONObject firstItem = resultArray.getJSONObject(0); 
JSONArray menuItems = firstItem.getJSONArray("menu_items"); 

0

JSONARRAY subArrayObj = innerObj1.getJSONARRAY( 「menu_items」);

由於menu_items是返回一個array..it應該在一個數組對象收集..

+0

此行顯示錯誤 –

+0

JSONObject innsersubObj = subArrayObj.getJSONObject(「item」); –

+0

是的,它會顯示一個錯誤..的Bec你必須使用一個數組的索引,那麼你可以獲取「項」 .. 您可以使用一個for循環,並使用獲得的JSONObject(I)的方法來做到這一點.. 然後內環u能「品」,這將再次返回一個數組.. 所以,基本上通過看什麼它返回的RHS使用正確的類。 – Ankit