1

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"}]

4
  • have you created a domain/class to serialize above JSON to class object?? Which gramework you're using?? Commented Aug 17, 2017 at 4:41
  • i am using normal jsp page and receiving data in java servlet. i dont know how to decode this string. please help.please suggest some way out. stuck on this since days. Commented Aug 17, 2017 at 4:43
  • how about jackson as a json parser? check the url wiki.fasterxml.com/JacksonInFiveMinutes. also maven build available. Commented Aug 17, 2017 at 4:46
  • @swap add full code , share us where you are getting your request.. You can achieve this using Jackson Commented Aug 17, 2017 at 4:53

1 Answer 1

1

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..

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for help.but i am not able to understand how to insert these obtained strings this into database in batch update.
do you use only pure jsp technique or use some framework like spring?
thank you for taking time and helping. your solution did give me the idea and direction to parse JSON.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.