2013-02-18 92 views
1

我似乎無法得到JSON數組中的第二層。 我想要的是,從類別中獲取所有名稱,然後我想通過意圖將「論壇ID」發送到不同的活動。
如果有幫助,我跟着這個tutorial,我讓我的JSON數組從here我如何獲得已經在JSONArray中的JSONArray

部分JSON陣列的

{ 
"categories": [{ 
    "name": "Facepunch", 
    "forums": [{ 
     "id": 6, 
     "name": "General Discussion", 
     "viewing": 217 
    }, { 
     "id": 60, 
     "name": "Fast Threads", 
     "viewing": 188 
    }, { 
     "id": 64, 
     "name": "Videos And Flash Movies and That Kind Of Crap", 
     "viewing": 239 
    }, { 
     "id": 403, 
     "name": "Mass Debate", 
     "viewing": 9 
    }, { 
     "id": 396, 
     "name": "Sensationalist Headlines", 
     "viewing": 455 
    }, { 
     "id": 51, 
     "name": "In The News Node", 
     "viewing": 100 
    }] 
    }] 
} 

我的代碼部分

try { 
    // Getting Array of Contacts 
categories = json.getJSONArray(TAG_CATEGORIES); 
forums = json.getJSONArray(TAG_FORUMS); 

// looping through All categories 
for(int i = 0; i < categories.length(); i++){ 
    JSONObject c = categories.getJSONObject(i); 


    // Storing each json item in variable 
    String CatName = c.getString(TAG_CATName); 

    // Phone number is agin JSON Object 
      JSONObject All_forums = forums.getJSONObject(i); 
      String forum_id = All_forums.getString(TAG_FORUMS_ID); 
      String forum_name = All_forums.getString(TAG_FORUMS_NAME); 

     // creating new HashMap 
     HashMap<String, String> map = new HashMap<String, String>(); 

     // adding each child node to HashMap key => value 
    map.put(TAG_CATName, CatName); 
    map.put(TAG_FORUMS, forum_name); 


    // adding HashList to ArrayList 
    contactList.add(map); 
} 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

回答

2

爲了讓你有一個類論壇的陣列來調用

forums = c.getJSONArray(TAG_FORUMS); 

您的for循環中。之後,您可以遍歷論壇來獲取他們的ID和名稱。

代碼示例:

categories = json.getJSONArray(TAG_CATEGORIES); 
for(int i = 0; i < categories.length(); i++) { 
    JSONObject c = categories.getJSONObject(i); 
    forums = c.getJSONArray(TAG_FORUMS); 
    for(int j = 0; j < forums.length(); j++){ 
     JSONObject f = forums.getJSONObject(j); 
     String forum_id = f.getString(TAG_FORUMS_ID); 
     String forum_name = f.getString(TAG_FORUMS_NAME); 
     … 
    } 
} 
+0

當我作出這樣的循環,它似乎只輸出JSON數組(稱爲嗜好)中的最後一項。即使是第一個循環內部的循環。 – dasmikko 2013-02-18 14:59:17

+0

也許你正在以錯誤的方式存儲值?嘗試通過在內部循環內添加一個* System.out.println(forum_name)*來進行調試。 – nutlike 2013-02-18 15:05:56

+0

我顯然保持覆蓋鍵「名字」當我做內循環,當我映射HashMap中的鍵。 – dasmikko 2013-02-18 15:58:22

0

你應該使用Gson解析器。它非常簡單和基本的用法比Android標準JSON解析器。你基本上解析。

爲其中已存在json對象項的類創建對象或原始類型。

+0

是否有任何容易理解的GSON教程? – dasmikko 2013-02-18 14:30:20

+0

public String getClasstoJson(YourClass o)throws Exception \t { \t \t Gson gson = new Gson(); \t \t gson.toJson(o); \t \t return gson。的toString(); \t} \t公共對象getJsontoClass(YourClass O,字符串JSON)拋出異常 \t { \t \t GSON GSON =新GSON(); \t \t嘗試{ \t \t \t Object st = o.getClass()。newInstance(); \t \t \t st = gson.fromJson(json.toString(),st.getClass()); \t \t \t return st; \t \t}趕上(IllegalAccessException E){ \t \t \t // TODO自動生成的catch程序塊 \t \t \t e.printStackTrace(); \t \t}趕上(InstantiationException E){ \t \t \t // TODO自動生成的catch程序塊 \t \t \t e.printStackTrace(); \t \t} \t \t return null; \t} – nurisezgin 2013-02-18 14:40:28

1

你必須得到來自該類別的JSONObject第二層。

try { 
// Getting Array of Contacts 
categories = json.getJSONArray(TAG_CATEGORIES); 

// looping through All categories 
for(int i = 0; i < categories.length(); i++){ 
    JSONObject c = categories.getJSONObject(i); 

    forums = c.getJSONArray(TAG_FORUMS); 

    ... 
} 
0

嘗試這種情況:
//聯繫人的獲取陣列
類別= json.getJSONArray(TAG_CATEGORIES);
forums = categories.getJSONArray(TAG_FORUMS);

//通過所有種類循環
對(INT I = 0;我< forums.length();我++){
的JSONObject C = forums.getJSONObject(ⅰ);
//你的代碼....
}

0

簡單的用法

public String getClasstoJson(Object o) throws Exception 
{ 
    Gson gson = new Gson(); 
    gson.toJson(o); 
    return gson.toString(); 
} 

public Object getJsontoClass(Object o,String json) throws Exception 
{ 

    Gson gson = new Gson(); 

    try { 
     Object st = o.getClass().newInstance(); 
     st= gson.fromJson(json.toString(), st.getClass()); 
     return st; 

    }catch (IllegalAccessException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (InstantiationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return null; 
}