【问题标题】:Parsing Dynamic JSON node with retrofit gson使用改造 gson 解析动态 JSON 节点
【发布时间】:2018-04-13 15:10:29
【问题描述】:

我正在使用 Woo-Commerce RestApi v2,使用 Retrofit 进行 api 调用。当我获取所有类别时。有一个节点名称image,它返回空白数组,但是当图像上传时它返回对象。这是我的 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) {

        }
    });

【问题讨论】:

    标签: android retrofit2 gson


    【解决方案1】:

    试试这个。

    Image 用作String

    类别

    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;
    }
    }
    

    用于响应

    @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();
    }
    

    如果它包含[,你不能做任何事情。

    否则你可以在你的代码中解析它。

    【讨论】:

    • 看起来很优雅的解决方案。测试后我会接受这个答案。谢谢
    【解决方案2】:

    您可以通过删除@Expose 注释并在Gson 构造函数中使用Gson gson = new GsonBuilder().create(); 来做到这一点。无根据的数据将设置为null

    【讨论】:

    猜你喜欢
    • 2017-02-28
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    相关资源
    最近更新 更多