Json object from database in java

Viewed 55858

Can anyone help me how to create a JSON Object from the database?

This is what the JSON output should look like:

{“devicelist”:{
    “device”: [
    {“id”: “01”, “type”: “CAM”, “name”: “Livingroom”}
    {“id”: “15”, “type”: “CAM”, “name”: “Kitchen”}
]
}}

This is my code:

 if (reg!=null)
 {

     try
                  {
                     con = ds.getConnection();
                     Statement select = con.createStatement();
                    ResultSet result=select.executeQuery("Select type,name,demo from register_device");  
                      while (result.next())
                      {
                         String  type_json=result.getString("type");
                         String name_json=result.getString("name");
                         String id_json=result.getString("demo");
                         JSONArray arrayObj=new JSONArray();

                      }
                  }
                  catch(Exception e)
                  {

                  }
      }

I am able to get the selected type,name,demo from the database.

I don't know how to start the JSON coding.

4 Answers
Related