【问题标题】:Wordpress API - How to loop through a JSON Array of objects in Android/JavaWordpress API - 如何在 Android/Java 中遍历 JSON 对象数组
【发布时间】:2017-07-24 16:51:56
【问题描述】:

我是 JSON 新手,在我正在构建的 Android 应用程序中循环遍历 JSONArray 中的 JSONObjects 时遇到了困难。如果发帖者也能对解决方案进行简要说明,我将不胜感激,因为我只是不想复制和粘贴代码,但要理解它。谢谢。

API 链接: https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories

API 输出:

[
{
    "id": 5580,
    "count": 5,
    "description": "",
    "link": "https://spoketest.wordpress.com/category/diversions/",
    "name": "Diversions",
    "slug": "diversions",
    "taxonomy": "category",
    "parent": 0,
    "meta": [],
    "_links": {
        "self": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/5580"
            }
        ],
        "collection": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
            }
        ],
        "about": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
            }
        ],
        "wp:post_type": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=5580"
            }
        ],
        "curies": [
            {
                "name": "wp",
                "href": "https://api.w.org/{rel}",
                "templated": true
            }
        ]
    }
},
{
    "id": 9548,
    "count": 5,
    "description": "",
    "link": "https://spoketest.wordpress.com/category/features/",
    "name": "Features",
    "slug": "features",
    "taxonomy": "category",
    "parent": 0,
    "meta": [],
    "_links": {
        "self": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/9548"
            }
        ],
        "collection": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
            }
        ],
        "about": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
            }
        ],
        "wp:post_type": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=9548"
            }
        ],
        "curies": [
            {
                "name": "wp",
                "href": "https://api.w.org/{rel}",
                "templated": true
            }
        ]
    }
},
{
    "id": 103,
    "count": 10,
    "description": "",
    "link": "https://spoketest.wordpress.com/category/news/",
    "name": "News",
    "slug": "news",
    "taxonomy": "category",
    "parent": 0,
    "meta": [],
    "_links": {
        "self": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/103"
            }
        ],
        "collection": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
            }
        ],
        "about": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
            }
        ],
        "wp:post_type": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=103"
            }
        ],
        "curies": [
            {
                "name": "wp",
                "href": "https://api.w.org/{rel}",
                "templated": true
            }
        ]
    }
},
{
    "id": 660,
    "count": 10,
    "description": "",
    "link": "https://spoketest.wordpress.com/category/people/",
    "name": "People",
    "slug": "people",
    "taxonomy": "category",
    "parent": 0,
    "meta": [],
    "_links": {
        "self": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/660"
            }
        ],
        "collection": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
            }
        ],
        "about": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
            }
        ],
        "wp:post_type": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=660"
            }
        ],
        "curies": [
            {
                "name": "wp",
                "href": "https://api.w.org/{rel}",
                "templated": true
            }
        ]
    }
},
{
    "id": 318,
    "count": 10,
    "description": "",
    "link": "https://spoketest.wordpress.com/category/tech/",
    "name": "Tech",
    "slug": "tech",
    "taxonomy": "category",
    "parent": 0,
    "meta": [],
    "_links": {
        "self": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/318"
            }
        ],
        "collection": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
            }
        ],
        "about": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
            }
        ],
        "wp:post_type": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=318"
            }
        ],
        "curies": [
            {
                "name": "wp",
                "href": "https://api.w.org/{rel}",
                "templated": true
            }
        ]
    }
},
{
    "id": 160479,
    "count": 6,
    "description": "",
    "link": "https://spoketest.wordpress.com/category/traction/",
    "name": "Traction",
    "slug": "traction",
    "taxonomy": "category",
    "parent": 0,
    "meta": [],
    "_links": {
        "self": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/160479"
            }
        ],
        "collection": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
            }
        ],
        "about": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
            }
        ],
        "wp:post_type": [
            {
                "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=160479"
            }
        ],
        "curies": [
            {
                "name": "wp",
                "href": "https://api.w.org/{rel}",
                "templated": true
            }
        ]
    }
}
]

我的代码:

import org.json.JSONException;
import org.json.JSONObject;

public class Category {
private int mCategoryID;
private String mCategoryName;

public static Category fromJSON(JSONArray jsonArray) {
    try {
        Category category = new Category();
        category.mCategoryID = jsonArray.getJSONObject("0").getInt("id");
        category.mCategoryName = jsonArray.getJSONObject("0").getString("name");

        return category;
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
}

public int getCategoryID() {
    return mCategoryID;
}

public String getCategoryName() {
    return mCategoryName;
}
}

【问题讨论】:

    标签: java android json wordpress


    【解决方案1】:

    从 API 检索到响应后(使用 AsyncTask),获取响应文本并创建一个 JSONArray。然后你可以遍历数组并提取类别。

    public void onApiResponse(String response) {
        // Create a JSONArray from the response
        JSONArray jsonArray = new JSONArray(response);
    
        // Create a structure to store all of the categories
        List<Category> categories = new ArrayList<Category>();
    
        // Loop through the array and parse all the categories
        for (int i = 0; i < jsonArray.length(); i++) {
            // Extract the JSONObject from the index
            JSONObject jsonCategory = jsonArray.getJSONObject(i);
    
            // Add the category to the list
            categories.add(Category.fromJSON(jsonCategory));
        }
    
        // Do something with your categories...
    }
    

    编辑:我看到你将一些值硬编码到你的 fromJSON 中,所以我想我也会发布一个更正的版本。

    public static Category fromJSON(JSONObject jsonObject) {
        try {
            Category category = new Category();
            category.mCategoryID = jsonObject.getInt("id");
            category.mCategoryName = jsonObjectgetString("name");
    
            return category;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }
    

    【讨论】:

    • 我正在使用 James Smith 的 Android 异步 Http 客户端库执行异步相关任务 (link),它返回 JSONObject。我的 fromJSON 方法将该 JSONObject 作为解析数据的输入参数。现在在您的解决方案中,我需要一个字符串作为输入参数。我可以使用 toString() 将 JSONObject 转换为 String 而不会产生任何不利影响吗?
    • 您发布的示例响应是 JSON 数组,而不是 JSON 对象。如果您收到 JSON 对象作为响应,则说明库没有正确解释响应,或者您错误地解释了返回值。打印出 JSONObjecttoString() 并与 API 中的实际文本进行比较,看看出了什么问题。
    • 哦!我犯了一个错误。是的,我得到一个 JSONArray,fromJSON 的输入参数是 JSONArray。但同样,我可以在 JSONArray 上使用 toString 而不会产生任何不利影响吗?
    • 理论上,你应该可以做到,但这会带来很多低效率的问题,这是可以避免的。花点时间根据你的代码调整我的答案,不要只是复制和粘贴。
    • 好的!会做。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-02-27
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多