title: Json object from database in java
body: <p>Can anyone help me how to create a JSON Object from the database?</p>
  
  <p>This is what the <strong>JSON output</strong> should look like:</p>
  
  <pre><code>{“devicelist”:{
      “device”: [
      {“id”: “01”, “type”: “CAM”, “name”: “Livingroom”}
      {“id”: “15”, “type”: “CAM”, “name”: “Kitchen”}
  ]
  }}
  </code></pre>
  
  <p>This is <strong>my code</strong>:</p>
  
  <pre><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)
                    {
  
                    }
        }
  </code></pre>
  
  <p>I am able to get the selected <strong>type,name,demo</strong> from the database.</p>
  
  <p>I don't know how to start the JSON coding.</p>