自定义解串器或不同的 class 设计改造

Custom deserializer or different class design Retrofit

Retrofit 让像我这样的菜鸟可以轻松上手。但是,我为当前项目请求的 API 响应结构与我之前使用的格式不同。我不确定我是否需要重写我的 POJO 或在 GSON 中制作自定义反序列化器。我无法更改 JSON 结构,自定义解串器对我来说似乎令人生畏。

这里是 JSON:

        {
    "Green Shirt": [
        {
            "id": "740",
            "name": “Nice Green Shirt",
            "quantity": "0",
            "make": "",
            "model": "",
            "price": “15.00",
            "size": "XXS",
            "sku": null,
            "image": "https:\/\/google.com\/green_shirt.jpg",
            "new_record": false,
            "category_name": "",
            "bar_code": "",
        },
        {
            "id": "743",
            "name": "Green Shirt",
            "quantity": “68",
            "make": "",
            "model": "",
            "price": “20.00",
            "size": "XS",
            "sku": null,
            "image": "https:\/\/google.com\/green_shirt.jpg",
            "new_record": false,
            "category_name": "",
            "bar_code": "",
        }
    ],
    "Dark Blue Jeans": [
        {
            "id": "1588",
            "name": "Dark Blue Jeans",
            "quantity": "0",
            "make": "",
            "model": "",
            "price": "0.00",
            "size": “S",
            "sku": null,
            "image": "https:\/\/google.com\/dark_blue_jeans.jpg",
            "new_record": false,
            "category_name": "",
            "bar_code": "",
            "category": null
        },
        {
            "id": "1559",
            "name": "Dark Blue Jeans",
            "quantity": "4",
            "make": "",
            "model": "",
            "price": "0.00",
            "size": “XL",
            "sku": null,
            "image": "https:\/\/google.com\/dark_blue_jeans.jpg",
            "new_record": false,
            "category_name": "",
            "bar_code": "",
            "category": null
        }
    ],
    "White Belt": [
        {
            "id": "1536",
            "name": "White Belt",
            "quantity": "37",
            "make": "",
            "model": "",
            "price": "0.00",
            "size": "One Size",
            "sku": null,
            "image": "https:\/\/google.com\/white_belt.jpg",
            "new_record": false,
            "category_name": "",
            "bar_code": "",
            "category": null
        }
    ]
}

这是 POJO:

    public class Product
{
    private String model;

    private String bar_code;

    private String image;

    private null sku;

    private String new_record;

    private String size;

    private String id;

    private null category;

    private String price;

    private String category_name;

    private String name;

    private String quantity;

    private String make;

    public String getModel ()
    {
        return model;
    }

    public void setModel (String model)
    {
        this.model = model;
    }

    public String getBar_code ()
    {
        return bar_code;
    }

    public void setBar_code (String bar_code)
    {
        this.bar_code = bar_code;
    }

    public String getImage ()
    {
        return image;
    }

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

    public null getSku ()
    {
        return sku;
    }

    public void setSku (null sku)
    {
        this.sku = sku;
    }

    public String getNew_record ()
    {
        return new_record;
    }

    public void setNew_record (String new_record)
    {
        this.new_record = new_record;
    }

    public String getSize ()
    {
        return size;
    }

    public void setSize (String size)
    {
        this.size = size;
    }

    public String getId ()
    {
        return id;
    }

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

    public null getCategory ()
    {
        return category;
    }

    public void setCategory (null category)
    {
        this.category = category;
    }

    public String getPrice ()
    {
        return price;
    }

    public void setPrice (String price)
    {
        this.price = price;
    }

    public String getCategory_name ()
    {
        return category_name;
    }

    public void setCategory_name (String category_name)
    {
        this.category_name = category_name;
    }

    public String getName ()
    {
        return name;
    }

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

    public String getQuantity ()
    {
        return quantity;
    }

    public void setQuantity (String quantity)
    {
        this.quantity = quantity;
    }

    public String getMake ()
    {
        return make;
    }

    public void setMake (String make)
    {
        this.make = make;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [model = "+model+", bar_code = "+bar_code+", image = "+image+", sku = "+sku+", new_record = "+new_record+", size = "+size+", id = "+id+", category = "+category+", price = "+price+", category_name = "+category_name+", name = "+name+", quantity = "+quantity+", make = "+make+"]";
    }
}

这里是请求和Retrofit接口:

    public static void requestData(String username,String password) {

        RestAdapter.Builder builder = new RestAdapter.Builder()
                .setClient(new OkClient(new OkHttpClient()))
                .setEndpoint(ENDPOINT);

        if (username != null && password != null) {
            // concatenate username and password with colon for authentication
            final String credentials = username + ":" + password;

            builder.setRequestInterceptor(new RequestInterceptor() {
                @Override
                public void intercept(RequestFacade request) {
                    // create Base64 encodet string
                    String string = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
                    request.addHeader("Accept", "application/json");
                    request.addHeader("Authorization", string);
                }
            });
        }
        RestAdapter adapter = builder.build();


        ProductAPI api = adapter.create(ProductAPI.class);

        api.getInventory(new Callback<List<Product>>() {
            @Override
            public void success(List<Product> products, Response response) {
                Log.d(TAG, response.getUrl());
                Log.d(TAG, response.getReason());
                mInventory = product;
            }
            @Override
            public void failure(RetrofitError error) {
                Log.d(TAG,error.getMessage());
            }
        });
    }

public interface ProductAPI {
    @GET("/v2/get-inventory")
    public void getInventory(Callback<List<Product>> response);
}

这是我收到的错误,因为 JSON 以“{”而不是“[”开头 com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 $

BEGIN_OBJECT

问题与它所说的完全一样。响应是有数组的对象,不是直接数组:

{ [{},{},{}] }

如果响应是:

[{},{},{}]

它会起作用,但由于您无法更改响应格式,因此您应该更改 java。您应该拥有一个内部包含产品列表的对象,而不是期待一个产品列表。这就是改造将映射它的方式。

编辑:
响应实际上如下:

 {[],[],[]}

不是列表,是由三个List of Objects Product组成的对象

到对象的映射必须是:

public class Wrapper
{
   List<Product> WhiteBelt;
   List<Product> DarkBlueJeans;
   List<Product> GreenShirt;

}

类似的东西就是你的结构。我不相信这是对的,因为你通常想要一个列表而不是一个包含三个列表的对象 "hardcoded".

如果有帮助请告诉我

已编辑:

所以你需要这样的东西:

{
    "products":
     [{ "name" : "Green Shirt"
        "items":
         [{"id": "740",
           "name": “Nice Green Shirt",
           "quantity": "0",
           "make": "",
           "model": "",
           "price": “15.00",
           "size": "XXS",
           "sku": null,
           "image": "https:\/\/google.com\/green_shirt.jpg",
           "new_record": false,
           "category_name": "",
           "bar_code": ""
         }]
      }]
}

这将是一个接受产品列表的结构,并且在每个产品中您都有项目列表(您已经拥有的)。

在Java中:

public class ProductList
{
    List <ProductList> productList;
}

public class ProductList
{
    String name;
    List<Product> items;
} 

使用您的原始产品class

您的 JSON 无效,请先尝试正确格式化,然后再进行其他操作。

格式正确:

{
  "Green Shirt": [
    {
      "id": "740",
      "name": "Nice Green Shirt",
      "quantity": "0",
      "make": "",
      "model": "",
      "price": "15.00",
      "size": "XXS",
      "sku": null,
      "image": "https:\/\/google.com\/green_shirt.jpg",
      "new_record": false,
      "category_name": "",
      "bar_code": ""
    },
    {
      "id": "743",
      "name": "Green Shirt",
      "quantity": "68",
      "make": "",
      "model": "",
      "price": "20.00",
      "size": "XS",
      "sku": null,
      "image": "https:\/\/google.com\/green_shirt.jpg",
      "new_record": false,
      "category_name": "",
      "bar_code": ""
    }
  ],
  "Dark Blue Jeans": [
    {
      "id": "1588",
      "name": "Dark Blue Jeans",
      "quantity": "0",
      "make": "",
      "model": "",
      "price": "0.00",
      "size": "S",
      "sku": null,
      "image": "https:\/\/google.com\/dark_blue_jeans.jpg",
      "new_record": false,
      "category_name": "",
      "bar_code": "",
      "category": null
    },
    {
      "id": "1559",
      "name": "Dark Blue Jeans",
      "quantity": "4",
      "make": "",
      "model": "",
      "price": "0.00",
      "size": "XL",
      "sku": null,
      "image": "https:\/\/google.com\/dark_blue_jeans.jpg",
      "new_record": false,
      "category_name": "",
      "bar_code": "",
      "category": null
    }
  ],
  "White Belt": [
    {
      "id": "1536",
      "name": "White Belt",
      "quantity": "37",
      "make": "",
      "model": "",
      "price": "0.00",
      "size": "One Size",
      "sku": null,
      "image": "https:\/\/google.com\/white_belt.jpg",
      "new_record": false,
      "category_name": "",
      "bar_code": "",
      "category": null
    }
  ]
}

它有一些错误:

Error:Strings should be wrapped in double quotes.[Code 17, Structure 12]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 28]
Error:Invalid comma, expecting }.[Code 141, Structure 53]
Error:Expecting string, not }.[Code 8, Structure 54]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 67]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 79]
Error:Invalid comma, expecting }.[Code 141, Structure 104]
Error:Expecting string, not }.[Code 8, Structure 105]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 138]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 192]

我告诉 Retrofit(GSON?)寻找 Map<String,List<Product>> 而不是 List<Product>,它发现了,多么方便。 :

        api.getInventory(new Callback<Map<String,List<Product>>>() {
            @Override
            public void success(Map<String,List<Product>> products, Response response) {

                mInventory = products;
            }
            @Override
            public void failure(RetrofitError error) {
                Log.d(TAG,error.getMessage());
            }
        });

public interface ProductAPI {
    @GET("/v2/get-inventory")
    public void getInventory(Callback<Map<String,List<Product>>> response);
}