如何获取已存在于JSONArray中的JSONArray

时间:2013-02-18 14:22:51

标签: android json

我似乎无法获得JSON数组中的第二层。 我想要的是,从类别中获取所有名称,然后我想通过意图将“论坛ID”发送给不同的活动。
如果它有帮助,我跟着这个tutorial,我从here

获取我的JSON数组

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();
}

5 个答案:

答案 0 :(得分: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);
        …
    }
}

答案 1 :(得分: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); 

    ...
}

答案 2 :(得分:0)

你应该使用Gson解析器。它比Android标准JSON解析器更简单和基本的用法。你解析非常基本。

为其中已存在的json对象项创建对象或基本类型的类。

答案 3 :(得分:0)

试试这个:     
    //获取联系人数组     
    categories = json.getJSONArray(TAG_CATEGORIES);     
    forums = categories.getJSONArray(TAG_FORUMS);     

    //循环遍历所有类别     
    for(int i = 0; i&lt; forums.length(); i ++){     
        JSONObject c = forums.getJSONObject(i);     
    //你的代码......
}

答案 4 :(得分: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;
}