Java 自定义反序列化器或不同类的设计改进
对我这样的傻瓜来说,改装让事情变得如此简单。但是,我为当前项目请求的API响应结构与我以前使用的格式不同。我不确定是否需要重写POJO或在GSON中创建自定义反序列化程序。我无法更改JSON结构,自定义反序列化程序对我来说似乎令人望而生畏 以下是JSON:Java 自定义反序列化器或不同类的设计改进,java,android,gson,deserialization,retrofit,Java,Android,Gson,Deserialization,Retrofit,对我这样的傻瓜来说,改装让事情变得如此简单。但是,我为当前项目请求的API响应结构与我以前使用的格式不同。我不确定是否需要重写POJO或在GSON中创建自定义反序列化程序。我无法更改JSON结构,自定义反序列化程序对我来说似乎令人望而生畏 以下是JSON: { "Green Shirt": [ { "id": "740", "name": “Nice Green Shirt", "qu
{
"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+"]";
}
}
以下是请求和改装界面:
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);
}
publicstaticvoidrequestdata(字符串用户名、字符串密码){
RestAdapter.Builder=new RestAdapter.Builder()
.setClient(新的OkClient(新的OkHttpClient()))
.setEndpoint(端点);
如果(用户名!=null和密码!=null){
//将用户名和密码与冒号连接以进行身份验证
最终字符串凭据=用户名+“:”+密码;
setRequestInterceptor(新的RequestInterceptor(){
@凌驾
公共无效截获(请求门面请求){
//创建Base64编码字符串
String String=“Basic”+Base64.encodeToString(credentials.getBytes(),Base64.NO\u WRAP);
addHeader(“接受”、“应用程序/json”);
addHeader(“授权”,字符串);
}
});
}
RestAdapter adapter=builder.build();
ProductAPI=adapter.create(ProductAPI.class);
api.getInventory(新回调(){
@凌驾
public void成功(列出产品、响应){
Log.d(标记,response.getUrl());
Log.d(标记,response.getReason());
mInventory=产品;
}
@凌驾
公共无效失败(错误){
Log.d(标记,error.getMessage());
}
});
}
公共接口产品API{
@获取(“/v2/获取库存”)
公共库存(回调响应);
}
这是我得到的错误,因为JSON以“{”而不是“[”开头
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_数组,但在第1行第2列路径处为BEGIN_对象$问题似乎正是它所说的。响应是具有数组的对象,而不是直接具有数组的对象:
{ [{},{},{}] }
如果答复是:
[{},{},{}]
这是可行的,但由于您无法更改响应格式,因此您应该更改java。您不应该期望产品列表,而应该拥有一个包含产品列表的对象。这就是改型将映射它的方式
编辑:事实上,答覆如下:
{[],[],[]}
不是列表,是由三个对象列表组成的对象产品
到对象的映射必须是:
public class Wrapper
{
List<Product> WhiteBelt;
List<Product> DarkBlueJeans;
List<Product> GreenShirt;
}
这将是一个接受产品列表的结构,在每个prodcut中,您都有一个项目列表(您已经拥有的)
在Java中:
public class ProductList
{
List <ProductList> productList;
}
public class ProductList
{
String name;
List<Product> items;
}
公共类产品列表
{
列出产品清单;
}
公共类产品列表
{
字符串名;
清单项目;
}
使用原始产品类时,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]
我告诉改造公司(GSON?)寻找一个地图
,而不仅仅是一个列表
,它发现了这一点,多么方便。
:
api.getInventory(新回调(){
@凌驾
public void成功(地图产品、响应){
mInventory=产品;
}
@凌驾
公共无效失败(错误){
Log.d(标记,error.getMessage());
}
});
公共接口产品API{
@获取(“/v2/获取库存”)
公共库存(回调响应);
}
列表没有填充到我的新对象中有什么问题:公共类清单{public list getProducts(){return products;}public void setProducts(list products){this.products=products;}private list products;}
所以我做了“硬代码”解决方案,它填充了其中的所有列表和对象,这对我来说是一个巨大的进步。我确实需要一个可以容纳任意数量的产品列表的解决方案,因为真正的结构对于硬编码来说太庞大了,这使我觉得我必须深入到GSON,并找出如何制作自己的自定义反序列化程序。而谢谢你的帮助,我会给你一个投票,但我甚至还没有15个代表。我真的希望API能给出一个不同的响应结构,但改变它是我无法控制的。我不认为我能说服任何人在短期内改变它。尽管如此,我还是感谢所有的帮助。
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]
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);
}