java - JSONObject ["featured_image"] 不是字符串

标签 java json wordpress

大家好,我从 Wordpress REST APi 调用获取以下 json

   [
        {
            "ID": 248,
            "post_title": "test post",
            "post_author": "admin",
            "excerpt": "",
            "url": "http://localhost/wp-learning/?p=248",
            "permalink": "http://192.168.10.31/wp-learning/test-post/",
            "post_date": "2017-08-07 09:15:57",
            "comment_count": "2",
            "category": [
                {
                    "term_id": 1,
                    "name": "Uncategorized",
                    "slug": "uncategorized",
                    "term_group": 0,
                    "term_taxonomy_id": 1,
                    "taxonomy": "category",
                    "description": "",
                    "parent": 0,
                    "count": 1,
                    "filter": "raw",
                    "cat_ID": 1,
                    "category_count": 1,
                    "category_description": "",
                    "cat_name": "Uncategorized",
                    "category_nicename": "uncategorized",
                    "category_parent": 0
                }
            ],
            "views": "4",
            "featured_image": false
        },
        {
            "ID": 247,
            "post_title": "The Coolest Vanity Apps for You and Your Girls",
            "post_author": "admin",
            "excerpt": "",
            "url": "http://td_uid_79_598817d2cebf2",
            "permalink": "http://192.168.10.31/wp-learning/the-coolest-vanity-apps-for-you-and-your-girls/",
            "post_date": "2017-08-07 07:33:38",
            "comment_count": "0",
            "category": [
                {
                    "term_id": 15,
                    "name": "Beauty",
                    "slug": "beauty",
                    "term_group": 0,
                    "term_taxonomy_id": 15,
                    "taxonomy": "category",
                    "description": "On each category you can set a Category template style, a Top post style (grids) and a module type for article listing.",
                    "parent": 14,
                    "count": 17,
                    "filter": "raw",
                    "cat_ID": 15,
                    "category_count": 17,
                    "category_description": "On each category you can set a Category template style, a Top post style (grids) and a module type for article listing.",
                    "cat_name": "Beauty",
                    "category_nicename": "beauty",
                    "category_parent": 14
                }
            ],
            "views": "0",
            "featured_image": "http://192.168.10.31/wp-learning/wp-content/uploads/2017/08/3.jpg"
        }
]

我在获取标签名称“featured_image”时遇到问题。它是什么类型的数据类型?我试过了

jsonObj.getString("featured_image"); 

为此,它给了我错误 JSONObject["featured_image"] 不是字符串。 还有

resultsObj.getJSONObject("featured_image").toString()

为此,它给了我错误 JSONObject["featured_image"] 不是 JSONObject。

最佳答案

您正在执行 resultsObj.getJSONObject("featured_image").toString() ,它要求从“featured_image”处的键/值中获取 JSON 对象。

但是我们可以看到该值是一​​个字符串,或者一个 boolean 值:

"featured_image": "http://192.168.10.31/wp-learning/wp-content/uploads/2017/08/3.jpg"

"featured_image": false

因此,您根本不应该在此键上调用 getJSONObject()

关于java - JSONObject ["featured_image"] 不是字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45569985/

相关文章:

java - 如何将属性(例如枚举)绑定(bind)到不同类型的组件属性(例如每个枚举的图像)?

java - 如何在 Android 上使用 EditText 更改按钮的文本

wordpress - 谷歌云sftp的权限

ajax - 使用 qtranslate 联系表格 7

css - 长帖子标题

java - Jooq "generateJooq"在 Spring Boot 应用程序中导致 java.lang.ClassNotFoundException : jakarta. xml.bind.annotation.XmlSchema

java - 如何通过XPath获取另一个表的TD内的表

php - 存储网站数据 - JSON 与 SQL

javascript - 我想从数组中返回一个 JSON 对象

javascript - 在 Ajax 调用中传递 Json.Net 对象