Gson解析-改造- Android [已关闭]

kmynzznz  于 2022-11-06  发布在  Android
关注(0)|答案(2)|浏览(237)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

去年关闭了。
Improve this question

{
"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"
            }
        }
    }
]
}

Package 函数模型类(& M):

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

改造示例

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

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

以上代码不起作用。

daupos2t

daupos2t1#

我建议在这种情况下,你可以使用一些实用工具,如:https://www.jsonschema2pojo.org/
在这里你可以粘贴你的JSON。选择“源类型:“-〉JSON选择“注解样式:“-〉GSON
点击预览,您将看到以下POJO类结构,它将以正确方式生成

-----------------------------------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类,即关键字products所在的根类。

bvjveswy

bvjveswy2#

根据您发布的代码,类ProductWrapper需要一个Blogs列表,但是在传入的JSON中,有一个产品列表。因此,将mData更改为:private List<Product> mData;
另外,现在还不需要自定义反序列化。如上所述,您应该尝试使用一个工具。如果您想使用Kotlin,可以使用一个类似的工具:https://www.json2kotlin.com/

相关问题