Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:13:35+00:00 2026-06-14T18:13:35+00:00

I get json which I need to parse to object with GSON: product:{ product_type:assignment,

  • 0

I get json which I need to parse to object with GSON:

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

“product_profile” will be different object depend on what I get in “product_type”. I created classes for each of that objects – Assignment and Writing. They are child of ProductType class. I implement interface in Product class which should return right object – Assignment or Writing depend on what value is in “product_type”. My Product class

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;

    }

}

But when I try to parse json to object like this:

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

I get such a product object:

id=447 title=assignment profile=ProductType@e49f9fa

so “id” and “product_type” parse correctly, but it is not Assignment object in ProdutType.

My ProductType class:

   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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:13:36+00:00Added an answer on June 14, 2026 at 6:13 pm

    Looks fine. To get correct output you need to override toString() in the ProductType class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON which I get from web application. I try this parse
I need to call a web service to receive a JSON object which I'll
I'm using spring 3.1.2 and I need to parse a json object into POJO.
I get an array (containing text) called titleArray from json which I populate a
I am having trouble parsing my JSON which i get from javascript. The format
Consider a javascript code snippet, which uses JSON to get twitter followers count. The
I'm making call to Alfresco Webscripts which return JSON. I do this using GET
I have a manager class, which update json data from web, get values... The
I've got an MVC 3 web app and am returning a JSON object which
I need to get value from json. It works perfectly on returned objects, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.