私はWoo-Commerce RestApi v2を使用しており、Retrofit
を使用してapi呼び出しを行っています。私はすべてのカテゴリを取得しています。ノード名がimage
の場合、空の配列が返されますが、イメージがアップロードされるとオブジェクトが返されます。ここにJSONサンプルがあります。改造gsonを使用した動的JSONノードの解析
[
{
"id": 15,
"name": "Albums",
"slug": "albums",
"parent": 11,
"description": "",
"display": "default",
"image": [],
"menu_order": 0,
"count": 4
},
{
"id": 9,
"name": "Clothing",
"slug": "clothing",
"parent": 0,
"description": "",
"display": "default",
"image": {
"id": 730,
"date_created": "2017-03-23T00:01:07",
"date_created_gmt": "2017-03-23T03:01:07",
"date_modified": "2017-03-23T00:01:07",
"date_modified_gmt": "2017-03-23T03:01:07",
"src": "https://example.com/wp-content/uploads/2017/03/T_2_front.jpg",
"title": "",
"alt": ""
},
"menu_order": 0,
"count": 36
}
]
イメージノードの主な問題です。 そのノードのモデルクラスを作成する方法。 jsonschema2pojo.orgを使ってモデルクラスを生成しましたが、そのクラスは動作します。クラスからimage
を削除すると動作します。 私は何をすべきか教えてください。 ご協力いただければ幸いです。
ApiInterface apiService= ApiClient.getWooCommerceClient().create(ApiInterface.class);
Call<List<Category>> call=apiService.getAllCategories();
call.enqueue(new Callback<List<Category>>() {
@Override
public void onResponse(@NonNull Call<List<Category>> call, @NonNull Response<List<Category>> response) {
categoryList.addAll(response.body());
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(@NonNull Call<List<Category>> call, @NonNull Throwable t) {
}
});
エレガントなソリューションのように見えます。私はテスト後にこの答えを受け入れるだろう..ありがとう –