Gson 解析 - 改造 - Android

Gson parsing - retrofit - Android

{
"products": [{
        "electronics": {
            "productId": "A2",
            "title": "earphone",
            "productDescription": "Description",
            "imageUrls": {
                "url1": "url1",
                "url2": "url2",
                "url3": "url3"
            }
        },
        "electronics": {
            "productId": "A2",
            "title": "mic",
            "productDescription": "Description",
            "imageUrls": {
                "url1": "url1",
                "url2": "url2",
                "url3": "url3"
            }
        }
    }
]
}

包装器和模型类:

public class Product {

@SerializedName("productId")
private String productId;
@SerializedName("title")
private String title;
@SerializedName("productDescription")
private String productDescription;
@SerializedName("imageUrls")
private List<ImageUrl> imageUrls;

// Setters & Getters

}

public class ImageUrl {
@SerializedName("url1")
private String url1;
@SerializedName("url2")
private String url2;
@SerializedName("url3")
private String url3;
// Setters & Getters
}

public class ProductWrapper {

@SerializedName("electronics")
private List<Product> mData;

// Setters & Getters
}

解串器:

public class MyDeserializer<T> implements JsonDeserializer<T> {
@Override
public T deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    // Get the "products" element from the parsed JSON
    JsonElement products = jsonElement.getAsJsonObject().getAsJsonArray("products");

    // Deserialize it. You use a new instance of Gson to avoid infinite recursion
    // to this deserializer
    return new Gson().fromJson(products, type);
}
}

RetrofitInstance

Gson gson =
                new GsonBuilder()
                        .registerTypeAdapter(Blog.class, new MyDeserializer())
                        .create();

        retrofit = new Retrofit
                .Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

以上代码无效。

我建议在这种情况下您可以使用一些实用工具,例如:https://www.jsonschema2pojo.org/

您可以在此处粘贴您的 JSON。 Select“来源类型:”-> JSON Select“注释样式:”-> GSON

单击“预览”,您将拥有以下 POJO classes 结构。它会以正确的方式生成它。

-----------------------------------com.example.Electronics.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class Electronics {

@SerializedName("productId")
@Expose
private String productId;
@SerializedName("title")
@Expose
private String title;
@SerializedName("productDescription")
@Expose
private String productDescription;
@SerializedName("imageUrls")
@Expose
private ImageUrls imageUrls;

public String getProductId() {
return productId;
}

public void setProductId(String productId) {
this.productId = productId;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getProductDescription() {
return productDescription;
}

public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}

public ImageUrls getImageUrls() {
return imageUrls;
}

public void setImageUrls(ImageUrls imageUrls) {
this.imageUrls = imageUrls;
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class Example {

@SerializedName("products")
@Expose
private List<Product> products = null;

public List<Product> getProducts() {
return products;
}

public void setProducts(List<Product> products) {
this.products = products;
}

}
-----------------------------------com.example.ImageUrls.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class ImageUrls {

@SerializedName("url1")
@Expose
private String url1;
@SerializedName("url2")
@Expose
private String url2;
@SerializedName("url3")
@Expose
private String url3;

public String getUrl1() {
return url1;
}

public void setUrl1(String url1) {
this.url1 = url1;
}

public String getUrl2() {
return url2;
}

public void setUrl2(String url2) {
this.url2 = url2;
}

public String getUrl3() {
return url3;
}

public void setUrl3(String url3) {
this.url3 = url3;
}

}
-----------------------------------com.example.Product.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class Product {

@SerializedName("electronics")
@Expose
private Electronics electronics;

public Electronics getElectronics() {
return electronics;
}

public void setElectronics(Electronics electronics) {
this.electronics = electronics;
}

}

然后创建 Retrofit 实例,您只需要这个。

 retrofit = new Retrofit
                .Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

在此示例中,您对改造调用的响应类型将是 Example class 根 class,其中键 products 所在。

根据您发布的代码,class ProductWrapper 需要一个博客列表,但在传入的 JSON 中有一个产品列表。 所以将 mData 更改为: private List<Product> mData;

此外,还不需要自定义反序列化。 如上所述,您应该尝试使用工具。 如果您想使用 Kotlin,可以使用类似的工具:https://www.json2kotlin.com/