1

我得到了需要用 GSON 解析为对象的 json:

"product":{
"product_type":"assignment",
"id":717,
    "product_profile":{
    "title":"new Order from java",
    "info":"Some special info",
    "dtl_expl":true,
    "special_info":""
    }
}

“product_profile”将是不同的对象,具体取决于我在“product_type”中得到的内容。我为每个对象创建了类——作业和写作。它们是 ProductType 类的子级。我在 Product 类中实现接口,该接口应该返回正确的对象 - 赋值或写入取决于“product_type”中的值。我的产品类别

public class Product implements IProductType{
    ProductAssignment prodAss;
    ProductWriting prodWr;
    ProductType returnState;

    @SerializedName("id")
    int id;
    @SerializedName("product_type")
    String product_type;
    @SerializedName("product_profile")
    ProductType product_profile;
    public Product()
    {

    }
    public Product(int id, String product_type, ProductType product_profile)
    {
        this.id = id;
        this.product_type = product_type;
        this.product_profile = product_profile;
    }
    public int getProductId() 
    {
         return this.id;
    }
    public String getProductType() 
    {
         return this.product_type;
    }
    public ProductType getProduct() 
    {
         return this.returnObject(product_type);
    }
    public void setProductId(int id) 
    {
        this.id = id;
    }
    public void setProductTitle(String product_type) 
    {
        this.product_type = product_type;
    }
    public void setProduct(ProductType product_profile) 
    {
        this.product_profile =  this.returnObject(product_type);
    }
    @Override
    public String toString() {
        return "id=" + id + " " + "title=" + product_type
                + " " + "profile=" + product_profile  +  "}";
    }
    @Override
    public ProductType returnObject(String res) 
    {

        System.out.println("Product");
        System.out.println(product_profile);
        if (res.equals("assignment"))
        {
            this.product_profile = new ProductAssignment();
            System.out.println("I'm going to parse Assignment");
        }
        else if  (res.equals("writing"))
            this.product_profile =  new ProductWriting();

        return product_profile;

    }

}

但是当我尝试将 json 解析为这样的对象时:

  Gson gson = new Gson();
  Product product = gson.fromJson(res,Product.class);

我得到这样一个产品对象:

id=447 title=assignment profile=ProductType@e49f9fa

所以“id”和“product_type”解析正确,但它不是ProdutType中的Assignment对象。

我的 ProductType 类:

   public class ProductType implements IProductType{
    String product;

    static ProductType productType;
    static ProductAssignment productAssignment;
    static ProductWriting productWriting;
    IProductType component;

    ProductType returnState;
    ProductAssignment prodAss;
    ProductWriting prodWr;
    public ProductType()
    {       
    }
    public ProductType(IProductType c)
    {
         component = c;
    }
    public ProductType getProductType()
    {
        return this.returnState;
    }
    public void setProductType(ProductType returnState)
    {
        this.returnState = returnState;
    }
    @Override
    public ProductType returnObject(String product_type) 
    {
        if (product_type.equals("assignment"))
        {
            this.returnState = new ProductAssignment();
            System.out.println("I'm going to parse Assignment");
        }
        else if  (product_type.equals("writing"))
            this.returnState =  new ProductWriting();

        return returnState;
    }
    @Override
    public String toString()
    {
        return returnState.getClass().getName();
    }

}
4

1 回答 1

0

看起来不错。要获得正确的输出,您需要toString()ProductType类中覆盖。

于 2012-11-21T09:57:11.653 回答