カスタムJSONレスポンスからWP V2 APIのWordPress標準JSONレスポンスに変換する方法について質問があります。JSONレスポンスをカスタムからWP API V2に切り替える
マイカスタムJSONレスポンスは次のようになります - 私のコードは、この
// 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応答は次のようになりますように見えます
{
"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
}
}
内枠です:
[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レスポンスにはcategoriesというカテゴリがありません。このため、私はthiを使用できません声明。
// Parsing the json response
JSONArray entry = response.getJSONArray(TAG_CATEGORIES);
私はボレーライブラリや例については、任意の良いドキュメントを見つけることができない、私の現在のコードは、URLからオブジェクトを取得し、その後のコードは、オブジェクトから配列を取得するためにresponse.getJSONArrayを使用しています。
オブジェクト内の配列を特定できない場合はどうすればいいですか、オブジェクトから配列への要求を変更する必要がある場合、取得するjson応答は配列です。私はこのことについて少し迷っているとは思わない。助けをお待ちしています。
おかげJsonObjectRequestから変更後どのように見えるかですが、私はJsonArrayRequestとでjsonObjectRequestを置き換えますそれは今正しく働いています。 – user1557970
あなたは歓迎です – Botz