2016-11-09 201 views
-1

我想解析'觀衆'數組。在JSON中解析嵌套數組Java

String data = { 
    "meta":{ 
    "requests":31454, 
    "timestamp":1456706180, 
    "status":200, 
    "message":"Request successful", 
    "method_id":1129, 
    "method":{ 

    } 
    }, 
    "data":[ 
    { 
     "id":3681, 
     "employer":"Google", 
     "date":"2016-01-05", 
     "day":"January", 
     "start_time":"17:00", 
     "end_time":"19:00", 
     "description":"Info session to discuss options at Google for majors outside of Computer Science.", 
     "website":"http:\/\/www.google.com\/careers\/students", 
     "building":{ 
     "code":"FED", 
     "name":"Federation Hall", 
     "room":"Main Hall", 
     "latitude":43.4732, 
     "longitude":-80.5485, 
     "map_url":"https:\/\/uwaterloo.ca\/map\/FED?basemap=D#map=17\/43.4732\/-80.5485" 
     }, 
     "audience":[ 
     "ENG - Computer", 
     "ENG - Electrical", 
     "ENG - Mechatronics", 
     "ENG - Software", 
     "ENG - System Design", 
     "MATH - Combinatorics & Optimization", 
     "MATH - Computer Science", 
     "MATH - Pure Mathematics" 
     ], 
     "link":"http:\/\/www.ceca.uwaterloo.ca\/students\/hiresessions_details.php?id=3681" 
    } 
     } 

我開始與

JSONObject obj = new JSONObject(data); 
     JSONArray data2 = new JSONArray(obj); 

但是我不確定現在該怎麼獲得 「觀衆」 的數據?我試圖從data2創建另一個數組,但是這給了一個錯誤,所以我真的不知道如何繼續?

+0

解釋downvote。那只是愚蠢的 – DilllyBar

+0

Downvotes不是愚蠢的。離開也許你會得到一個錯誤。所以不會閱讀[如何在Android中解析JSON](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) –

回答

0

良好的開端

JSONObject obj = new JSONObject(data); 

那麼,這就是你需要得到"data"陣列出來的,而不是讓從物體new數組的對象。

JSONArray data = obj.getJsonArray("data"); 

一旦你有一個數組,你可以遍歷與for循環有規律的,最多i < data.length()

當你遍歷數組,你有你需要得到

JSONObject inner = data.getJSONObject(i); 

而那些有觀衆陣列更JSON對象。

JSONArray audience = inner.getJSONArray("audience"); 

隨意use this site和GSON或傑克遜庫,以幫助您解析努力