大家好,我从 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/