JSONObject [" featured_image"]不是String

时间:2017-08-08 13:40:29

标签: java json wordpress

大家好我从Wordpress得到了一个跟随json的APi Call

   [
        {
            "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。

4 个答案:

答案 0 :(得分:0)

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

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

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

"featured_image": false

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我认为它内在的json问题 试试这样:

JSONObject category = jsonObj.getJSONObject("category");

category.getJSONObject("featured_image");

答案 3 :(得分:0)

您可以使用以下代码,我没有经过测试,但这有助于确定数据类型,并根据您可以决定需要调用哪种方法的类型。

所以使用:

JsonValue type = resultsObj.get("featured_image").getValueType();
if (type.getValueType() == JsonValue.ValueType.TRUE || type.getValueType() == JsonValue.ValueType.FALSE){
 // call boolean method
  boolean bValue = resultsObj.get("featured_image").getAsBoolean()

}else{
   //based on you type you can call respective method.
}

请参阅以下链接: JsonValueType

getValueType

相关问题