我有一个关于从自定义JSON响应转换为WP V2 API的WordPress标准JSON响应的问题。
我的自定义json响应看起来像这样 - 它是在
中构建的 {
"categories":
[26]
0:
{
"term_id": 12
"name": "Android"
"slug": "android"
"term_group": 0
"term_taxonomy_id": 12
"taxonomy": "category"
"description": ""
"parent": 463
"count": 10
"filter": "raw"
"cat_ID": 12
"category_count": 10
"category_description": ""
"cat_name": "Android"
"category_nicename": "android"
"category_parent": 463
}
}
我的代码看起来像这样
// Preparing volley's json object request
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
List<Category> categories = new ArrayList<Category>();
try {
if (response.has("error")) {
String error = response.getString("error");
Toast.makeText(getApplicationContext(), error, Toast.LENGTH_LONG).show();
}else {
// Parsing the json response
JSONArray entry = response.getJSONArray(TAG_CATEGORIES);
// loop through categories and add them to
// list
for (int i = 0; i < entry.length(); i++) {
JSONObject catObj = (JSONObject) entry.get(i);
// album id
String catID = catObj.getString(TAG_TERM_ID);
// album title
String catTitle = catObj.getString(TAG_TERM_NAME);
Category category = new Category();
category.setId(catID);
category.setTitle(catTitle);
// add album to list
categories.add(category);
}
WordPress RST API V2的JSON响应如下所示:
[10]
0: {
"id": 12
"count": 10
"description": ""
"link": "https://torbjornzetterlund.com/category/technology/mobile-apps/android/"
"name": "Android"
"slug": "android"
"taxonomy": "category"
"parent": 463
"img": false
"_links": {
"self": [1]
0: {
"href": "https://torbjornzetterlund.com/wp-json/wp/v2/categories/12"
}-
- "collection": [1]
0: {
"href": "https://torbjornzetterlund.com/wp-json/wp/v2/categories"
}-
- "about": [1]
0: {
"href": "https://torbjornzetterlund.com/wp-json/wp/v2/taxonomies/category"
}-
- "up": [1]
0: {
"embeddable": true
"href": "https://torbjornzetterlund.com/wp-json/wp/v2/categories/463"
}-
-"wp:post_type": [1]
0: {
"href": "https://torbjornzetterlund.com/wp-json/wp/v2/posts?categories=12"
}-
- "curies": [1]
0: {
"name": "wp"
"href": "https://api.w.org/{rel}"
"templated": true
}
}
}
}
新的JSO响应没有类别类别,因此我不能使用此声明。
// Parsing the json response
JSONArray entry = response.getJSONArray(TAG_CATEGORIES);
我找不到任何关于volley库或示例的好文档,我当前的代码从url获取一个对象,然后代码使用response.getJSONArray从对象中获取数组。
如果我无法在对象中识别数组,我需要做什么,我得到的json响应是一个数组,我应该将请求从对象更改为数组。我不确定这一点有点丢失。感谢任何帮助。
答案 0 :(得分:0)
您正在使用JsonObjectRequest,适合您的api,但现在它来自您的api的JsonArray。
Volley Toolbox中还有一个JsonArrayRequest。如果您更改了“请求类型”,那么它将起作用。
如果事情不明确,请随时提出。
答案 1 :(得分:0)
这是我从JsonObjectRequest更改为JsonArrayRequest后的代码片段的样子 JsonArrayRequest jsonArrReq = new JsonArrayRequest(url,new Response.Listener(){
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
List<Category> categories = new ArrayList<Category>();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject catObj = response.getJSONObject(i);
// album id
String catID = catObj.getString(TAG_TERM_ID);
// album title
String catTitle = catObj.getString(TAG_TERM_NAME);
Category category = new Category();
category.setId(catID);
category.setTitle(catTitle);
// add album to list
categories.add(category);
// Store categories in shared pref
AppController.getInstance().getPrefManger().storeCategories(categories);
// String the main activity
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {