首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSONObject["featured_image"]不是字符串

JSONObject["featured_image"]不是字符串
EN

Stack Overflow用户
提问于 2017-08-08 21:40:29
回答 4查看 1K关注 0票数 0

大家好,我从Wordpress rest APi调用中得到了下面的json。

代码语言:javascript
复制
   [
        {
            "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“时遇到问题。它是什么类型的数据类型?我试过了

代码语言:javascript
复制
jsonObj.getString("featured_image"); 

为此,它给了我错误JSONObject"featured_image“不是一个字符串。也是

代码语言:javascript
复制
resultsObj.getJSONObject("featured_image").toString()

为此,它给了我错误JSONObject"featured_image“不是一个JSONObject。

EN

回答 4

Stack Overflow用户

发布于 2017-08-08 21:43:26

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

但是我们可以看到这个值是一个字符串,或者是一个布尔值:

代码语言:javascript
复制
"featured_image": "http://192.168.10.31/wp-learning/wp-content/uploads/2017/08/3.jpg"

"featured_image": false

所以你根本不应该在这个键上调用getJSONObject()

票数 0
EN

Stack Overflow用户

发布于 2017-08-08 21:45:19

根据您的JSON结构,看起来"featured_image“是布尔型的,而不是字符串或对象。

票数 0
EN

Stack Overflow用户

发布于 2017-08-08 22:01:18

我认为它内部的json问题可以这样尝试:

代码语言:javascript
复制
JSONObject category = jsonObj.getJSONObject("category");

category.getJSONObject("featured_image");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45569985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档