swap
swap

Reputation: 11

Json String Parsing In Java Servlet And Inserting In Database

I am new to JSON. I have a dynamic html table in jsp which can add rows on a button click. I am able to receive a JSON string in servlet but not able to decode it. I want to split the string and insert data in a database. My JSON string looks like this:
[{"srno":"1","itemcode":"4545","type":"45454","subtype":"54","sku":"45","invqty":"54","invprice":"54","discount":"5","total":"45"},{"srno":"2","itemcode":"44","type":"54","subtype":"45","sku":"45","invqty":"4","invprice":"54","discount":"54","total":"5"}]

Upvotes: 1

Views: 813

Answers (1)

tommybee
tommybee

Reputation: 2551

I believe that this is one possible solution using jackson parser here.

You have to make your own class for the class and deserialise it if you want to use this library.

Your class might be..

class MyJsonClass
{
    private String srno;
    private String itemcode;
    private String type;
    private String subtype;
    private String sku;
    private String invqty;
    private String invprice;
    private String discount;
    private String total;

    public String getSrno(){ return srno;}
    public void setSrno(String srno){ this.srno = srno;}
    public String getItemcode(){ return itemcode;}
    public void setItemcode(String itemcode){ this.itemcode = itemcode;}
    public String getType(){ return type;}
    public void setType(String type){ this.type = type;}
    public String getSubtype(){ return subtype;}
    public void setSku(String sku){ this.sku = sku;}
    public String getSku(){ return sku;}
    public void setSubtype(String subtype){ this.subtype = subtype;}
    public String getInvqty(){ return invqty;}
    public void setInvqty(String invqty){ this.invqty = invqty;}
    public String getInvprice(){ return invprice;}
    public void setInvprice(String invprice){ this.invprice = invprice;}
    public String getDiscount(){ return discount;}
    public void setDiscount(String discount){ this.discount = discount;}
    public String getTotal(){ return total;}
    public void setTotal(String total){ this.total = total;}


    @Override
    public String toString()
    {
        StringBuffer stbuf = new StringBuffer();
        stbuf.append("srno : ").append(srno);
        stbuf.append(" itemcode : ").append(itemcode);
        stbuf.append(" type : ").append(type);
        stbuf.append(" subtype : ").append(subtype);
        stbuf.append(" sku : ").append(sku);
        stbuf.append(" invqty : ").append(invqty);
        stbuf.append(" invprice : ").append(invprice);
        stbuf.append(" discount : ").append(discount);
        stbuf.append(" total : ").append(total);

        return stbuf.toString();

    }
}

Then, main entry code might look like code below.

public class MyBizParser {
    public static void main(String[] args)
    {
        String yourJson = 
            "["
            + "{\"srno\":\"1\",\"itemcode\":\"4545\",\"type\":\"45454\",\"subtype\":\"54\",\"sku\":\"45\",\"invqty\":\"54\",\"invprice\":\"54\",\"discount\":\"5\",\"total\":\"45\"},"
            + "{\"srno\":\"2\",\"itemcode\":\"44\",\"type\":\"54\",\"subtype\":\"45\",\"sku\":\"45\",\"invqty\":\"4\",\"invprice\":\"54\",\"discount\":\"54\",\"total\":\"5\"}"
            + "]";

        parsingIt(yourJson);
    }

    private static void parsingIt(String yourJson) {
        ObjectMapper objMapper = new ObjectMapper();

        try {
            MyJsonClass[] myObjects = objMapper.readValue(yourJson, MyJsonClass[].class);

            for(int i = 0; i < myObjects.length; i++)
                System.out.println(myObjects[i]);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

done.. Well, the output must be enter image description here

I hope this is what you want..

Upvotes: 1

Related Questions