August 11, 2018

Serializing POJO to JSON - Input parameterization

How to output a json like below using java program



{"id":"0001","name":"Cake","topping":[{"id":"5001","type":"None"},{"id":"5004","type":"Maple"}],"ppu":"0.55","type":"donut","batters":{"batter":[{"id":"1004","type":"Devil's Food"},{"id":"0001","type":"Regular"}]}}

Program

/**
 * 
 */
package com.belazy.pojostructure;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;


/**
 * @author consumerfed IT Section 
 * Kozhikode
 *
 */
public class ManiClass {

public static void main(String[] args) throws JsonProcessingException {
Javabelazy user = new Javabelazy();
user.setId("0001");
    user.setType("donut");
    user.setName("Cake");
    user.setPpu("0.55");
    Batter batter1 = new Batter();
    batter1.setId("0001");
    batter1.setType("Regular");
    Batter batter2 = new Batter();
    batter2.setId("1004");
    batter2.setType("Devil's Food");
    Batters batters = new Batters();
    Batter[] batter = new Batter[2];
    batter[1] = batter1;
    batter[0] = batter2;
batters.setBatter(batter );
    user.setBatters(batters); 
    Topping topping1 = new Topping();
    topping1.setId("5001");
    topping1.setType("None");
    Topping topping2 = new Topping();
    topping2.setId("5004");
    topping2.setType("Maple");
    Topping[] topping = new Topping[2];
    topping[0] = topping1;
    topping[1] = topping2;
user.setTopping(topping );
ObjectMapper objectmapper = new ObjectMapper();
    String json = objectmapper.writeValueAsString(user);
    System.out.println(json);
}

}


User class

package com.belazy.pojostructure;
public class Javabelazy
{
    private String id;

    private String name;

    private Topping[] topping;

    private String ppu;

    private String type;

    private Batters batters;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }

    public Topping[] getTopping ()
    {
        return topping;
    }

    public void setTopping (Topping[] topping)
    {
        this.topping = topping;
    }

    public String getPpu ()
    {
        return ppu;
    }

    public void setPpu (String ppu)
    {
        this.ppu = ppu;
    }

    public String getType ()
    {
        return type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

    public Batters getBatters ()
    {
        return batters;
    }

    public void setBatters (Batters batters)
    {
        this.batters = batters;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", name = "+name+", topping = "+topping+", ppu = "+ppu+", type = "+type+", batters = "+batters+"]";
    }
}

Topping class

package com.belazy.pojostructure;
public class Topping
{
    private String id;

    private String type;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getType ()
    {
        return type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", type = "+type+"]";
    }
}


Batters class
package com.belazy.pojostructure;
public class Batters
{
    private Batter[] batter;

    public Batter[] getBatter ()
    {
        return batter;
    }

    public void setBatter (Batter[] batter)
    {
        this.batter = batter;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [batter = "+batter+"]";
    }
}

Batter class

package com.belazy.pojostructure;
public class Batter
{
    private String id;

    private String type;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getType ()
    {
        return type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", type = "+type+"]";
    }
}


Output



No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments