大家好,我从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。
发布于 2017-08-08 21:43:26
您正在执行resultsObj.getJSONObject("featured_image").toString(),它要求从'featured_image‘处的键/值中获取一个JSON对象。
但是我们可以看到这个值是一个字符串,或者是一个布尔值:
"featured_image": "http://192.168.10.31/wp-learning/wp-content/uploads/2017/08/3.jpg"
"featured_image": false所以你根本不应该在这个键上调用getJSONObject()。
发布于 2017-08-08 21:45:19
根据您的JSON结构,看起来"featured_image“是布尔型的,而不是字符串或对象。
发布于 2017-08-08 22:01:18
我认为它内部的json问题可以这样尝试:
JSONObject category = jsonObj.getJSONObject("category");
category.getJSONObject("featured_image");https://stackoverflow.com/questions/45569985
复制相似问题