WordPressAPI-如何在Android/Java中循环通过JSON对象数组
我是JSON新手,我正在构建一个Android应用程序,在JSONArray中通过JSONObject进行循环时遇到了困难。如果海报也能给大众一个简单的解释,我会很感激,因为我只是不想复制和粘贴代码,但能理解它。多谢各位 API链接: API输出:WordPressAPI-如何在Android/Java中循环通过JSON对象数组,java,android,json,wordpress,Java,Android,Json,Wordpress,我是JSON新手,我正在构建一个Android应用程序,在JSONArray中通过JSONObject进行循环时遇到了困难。如果海报也能给大众一个简单的解释,我会很感激,因为我只是不想复制和粘贴代码,但能理解它。多谢各位 API链接: API输出: [ { "id": 5580, "count": 5, "description": "", "link": "https://spoketest.wordpress.com/category/diversions/"
[
{
"id": 5580,
"count": 5,
"description": "",
"link": "https://spoketest.wordpress.com/category/diversions/",
"name": "Diversions",
"slug": "diversions",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/5580"
}
],
"collection": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
}
],
"about": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=5580"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
},
{
"id": 9548,
"count": 5,
"description": "",
"link": "https://spoketest.wordpress.com/category/features/",
"name": "Features",
"slug": "features",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/9548"
}
],
"collection": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
}
],
"about": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=9548"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
},
{
"id": 103,
"count": 10,
"description": "",
"link": "https://spoketest.wordpress.com/category/news/",
"name": "News",
"slug": "news",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/103"
}
],
"collection": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
}
],
"about": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=103"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
},
{
"id": 660,
"count": 10,
"description": "",
"link": "https://spoketest.wordpress.com/category/people/",
"name": "People",
"slug": "people",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/660"
}
],
"collection": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
}
],
"about": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=660"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
},
{
"id": 318,
"count": 10,
"description": "",
"link": "https://spoketest.wordpress.com/category/tech/",
"name": "Tech",
"slug": "tech",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/318"
}
],
"collection": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
}
],
"about": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=318"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
},
{
"id": 160479,
"count": 6,
"description": "",
"link": "https://spoketest.wordpress.com/category/traction/",
"name": "Traction",
"slug": "traction",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/160479"
}
],
"collection": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
}
],
"about": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=160479"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
}
]
import org.json.JSONException;
import org.json.JSONObject;
public class Category {
private int mCategoryID;
private String mCategoryName;
public static Category fromJSON(JSONArray jsonArray) {
try {
Category category = new Category();
category.mCategoryID = jsonArray.getJSONObject("0").getInt("id");
category.mCategoryName = jsonArray.getJSONObject("0").getString("name");
return category;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
public int getCategoryID() {
return mCategoryID;
}
public String getCategoryName() {
return mCategoryName;
}
}
我的代码:
[
{
"id": 5580,
"count": 5,
"description": "",
"link": "https://spoketest.wordpress.com/category/diversions/",
"name": "Diversions",
"slug": "diversions",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/5580"
}
],
"collection": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
}
],
"about": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=5580"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
},
{
"id": 9548,
"count": 5,
"description": "",
"link": "https://spoketest.wordpress.com/category/features/",
"name": "Features",
"slug": "features",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/9548"
}
],
"collection": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
}
],
"about": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=9548"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
},
{
"id": 103,
"count": 10,
"description": "",
"link": "https://spoketest.wordpress.com/category/news/",
"name": "News",
"slug": "news",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/103"
}
],
"collection": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
}
],
"about": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=103"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
},
{
"id": 660,
"count": 10,
"description": "",
"link": "https://spoketest.wordpress.com/category/people/",
"name": "People",
"slug": "people",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/660"
}
],
"collection": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
}
],
"about": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=660"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
},
{
"id": 318,
"count": 10,
"description": "",
"link": "https://spoketest.wordpress.com/category/tech/",
"name": "Tech",
"slug": "tech",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/318"
}
],
"collection": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
}
],
"about": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=318"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
},
{
"id": 160479,
"count": 6,
"description": "",
"link": "https://spoketest.wordpress.com/category/traction/",
"name": "Traction",
"slug": "traction",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/160479"
}
],
"collection": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories"
}
],
"about": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=160479"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
}
]
import org.json.JSONException;
import org.json.JSONObject;
public class Category {
private int mCategoryID;
private String mCategoryName;
public static Category fromJSON(JSONArray jsonArray) {
try {
Category category = new Category();
category.mCategoryID = jsonArray.getJSONObject("0").getInt("id");
category.mCategoryName = jsonArray.getJSONObject("0").getString("name");
return category;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
public int getCategoryID() {
return mCategoryID;
}
public String getCategoryName() {
return mCategoryName;
}
}
从API检索响应后(使用
AsyncTask
),获取响应文本并创建JSONArray
。然后可以循环遍历数组并提取类别
public void onApiResponse(String response) {
// Create a JSONArray from the response
JSONArray jsonArray = new JSONArray(response);
// Create a structure to store all of the categories
List<Category> categories = new ArrayList<Category>();
// Loop through the array and parse all the categories
for (int i = 0; i < jsonArray.length(); i++) {
// Extract the JSONObject from the index
JSONObject jsonCategory = jsonArray.getJSONObject(i);
// Add the category to the list
categories.add(Category.fromJSON(jsonCategory));
}
// Do something with your categories...
}
我使用James Smith的Android异步Http客户端库来执行异步相关任务(),它返回JSONObject。我的fromJSON方法将JSONObject作为解析数据的输入参数。现在在您的解决方案中,我需要一个字符串作为输入参数。我可以使用toString()将JSONObject转换为字符串而不产生任何不利影响吗?您发布的示例响应是JSON数组,而不是JSON对象。如果您在响应中接收到一个JSON对象,则表示库没有正确解释响应,或者您没有正确解释返回值。打印出
JSONObject
的toString()
,并与API中的实际文本进行比较,看看哪里出了问题。噢!我犯了一个错误。是的,我得到一个JSONArray,fromJSON的输入参数是JSONArray。但是再一次,我可以在JSONArray上使用toString而不产生任何负面影响吗?理论上,你应该可以,但这会带来很多可以避免的低效。花点时间让我的答案适应你的代码,不要只是复制和粘贴。好的!行。非常感谢。