2017-11-01 28 views
-1

私は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) { 

     } 
    }); 

答えて

1

これを試してください。それは[が含まれている場合には、すべてのことを行うことはできませんString応答

@Override 
public void onResponse(@NonNull Call<List<Category>> call, @NonNull Response<List<Category>> response) { 
    categoryList.addAll(response.body()); 
    for (int i = 0; i < categoryList.size(); i++) { 
     if(!categoryList.get(i).getImage().contains("[")){ 
      image = categoryList.get(i).getImage(); 
      try { 
       JSONObject jsonObject = new JSONObject(image); 
       String imageUrl = jsonObject.getString("your_key"); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    adapter.notifyDataSetChanged(); 
} 

カテゴリー

public class Category { 
/** 
* id : 15 
* name : Albums 
* slug : albums 
* parent : 11 
* description : 
* display : default 
* image : [] 
* menu_order : 0 
* count : 4 
*/ 

private int id; 
private String name; 
private String slug; 
private int parent; 
private String description; 
private String display; 
private int menu_order; 
private int count; 
private String image; 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getSlug() { 
    return slug; 
} 

public void setSlug(String slug) { 
    this.slug = slug; 
} 

public int getParent() { 
    return parent; 
} 

public void setParent(int parent) { 
    this.parent = parent; 
} 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public String getDisplay() { 
    return display; 
} 

public void setDisplay(String display) { 
    this.display = display; 
} 

public int getMenu_order() { 
    return menu_order; 
} 

public void setMenu_order(int menu_order) { 
    this.menu_order = menu_order; 
} 

public int getCount() { 
    return count; 
} 

public void setCount(int count) { 
    this.count = count; 
} 

public String getImage() { 
    return image; 
} 

public void setImage(String image) { 
    this.image = image; 
} 
} 

使用など

使用Image

これ以外の場合は、コード内で解析できます。

+0

エレガントなソリューションのように見えます。私はテスト後にこの答えを受け入れるだろう..ありがとう –

1

あなたは@Expose注釈を削除することによってそれを行うとGsonコンストラクタでGson gson = new GsonBuilder().create();を使用することができます。根拠のないデータはnullに設定されます。

+0

いくつかのコードスニペットを提供できますか。私はGSONを初めて使う人です。 –

+0

@AbhishekSinghこのリンクを参照してください:https://stackoverflow.com/questions/2864370/how-do-i-use-googles-gson-api-to-deserialize-json-properly – Ironman

関連する問題