2012-11-21 2 views
1

나는 GSON에 이의를 구문 분석 할 필요가 JSON을 얻을. 과제의 각각에 대해 수업을 작성했습니다. 과제 및 과제. 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을 구문 분석 할 때 :

그래서
id=447 title=assignment [email protected] 

"ID"와 "제품 _ :

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

이 나는 ​​그런 제품 객체를 가져 "제대로 구문 분석하지만 ProdutType의 할당 객체가 아닙니다.

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

} 
+1

'ProductType'은'toString()'을 대체합니까? – jlordo

+0

예, 방금했는데 ... ProductType 클래스에 문제가있을 수 있습니까? –

+0

최근 비슷한 문제가 발생했습니다. 나는 Deserializer를 사용하여 그것을 해결했다. (http://stackoverflow.com/a/12459070/823393). – OldCurmudgeon

답변

0

잘 보인다. 출력으로 바꾸려면 ProductType 클래스에서 toString()을 무시해야합니다.

+0

문자열로 대체하고 ProductAssignment를 반환하려고하면 ProductType 또는 returnObject 인터페이스의 ProductType 결과 중 하나가 NullPoinerException ... –

+0

ProductType 클래스를 제 질문에 추가했습니다. 제발, 아마도 그걸로 뭔가 잘못 됐나요? –

+0

'... "profile ="+ product_profile.getClass(). getName()''Produt' 클래스에서 인쇄 하시겠습니까? – jlordo