vivek singh
vivek singh

Reputation: 457

How to Insert JSON data into database

From my UI via ajax I am getting an JSON data Which I am trying to insert into my data base, about which I have no idea how can i do that, I know how to insert something in database using prepared statements but here in JSON case I am totally confused

This is my JSON which I am getting in my doPost

{"ImageData":[{"Counter":"Counter A","Name":"CountA1.jpg","IsActive":"Y"},{"Counter":"Counter A","Name":"CountA2.jpg","IsActive":"Y"}]}

servlet code -->

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        Scanner scanner = new Scanner(request.getInputStream());
            String imageData = scanner.nextLine();
    System.out.println(imageData);

}

now I want to store this in my db like This

Do i need to parse this JSON, how will i do ps.setInt(index, 0);, I am not getting any idea

Edit/update

As @Anjana Senanayake answer I am doing like this

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Scanner scanner = new Scanner(request.getInputStream());
        String imageData = scanner.nextLine();
        System.out.println(imageData);
        JSONObject jsonObj = new JSONObject(imageData);
        JSONArray jsonArray = jsonObj.getJSONArray("ImageData");

        JSONObject innerObj = new JSONObject(jsonArray); // this one is throwing error 
// eror is `The type of the expression must be an array type but it resolved to JSONArray`
        String counter = innerObj.getString("Counter");
        String name = innerObj.getString("Name");
        String isActive = innerObj.getString("IsActive");

        System.out.println(counter);

    }

But it is throwing error at JSONObject innerObj = new JSONObject(jsonArray[0]); error is The type of the expression must be an array type but it resolved to JSONArray

Upvotes: 0

Views: 6365

Answers (2)

Yuri
Yuri

Reputation: 1814

You have several options here. It depends on what you want to achieve :)

Quick and dirty

Save it as a string. You will have to create a new class with String object, then put your JSON data into this object and save it.

I realized that it's not going to work since it is not what you actually want :)

Better way to do that

Create a new class with all fields.

// File: ImageData.java
public class ImageData {
    private String counter;
    private String name;
    private String isActiveJpg;

    // getters and setters
}

// File: Data.java
public class Data {
    private List<ImageData> imageDataList;

    // getters and setters
}

Having those classes in place you are ready to use Jackson or GSON library. Please take a look at the usage example for Jackson here and GSON serialization and deserialization.

Sidenote

I would recommend to rethink the design itself. E.g. the naming of the fields and the logic of the entity is quite misleading (things like isActiveJpg. It should be a Set and it should have a better naming like isJpg or something like that).

Upvotes: 2

Anjana
Anjana

Reputation: 933

Use org.json library to parse it and create JsonObject :-

JSONObject jsonObj = new JSONObject(imageData);
JSONArray jsonArray = jsonObj.getJSONArray("ImageData");

Now, use this object to get your values :-

JSONObject innerObj = new JSONObject(jsonArray[0]);

String counter = innerObj.getString("Counter");
String name = innerObj.getString("Name");
String isActive = innerObj.getString("IsActive");

since it is an array of objects, you have do repeat above for all the elements in jsonObj.

Upvotes: 1

Related Questions