How to Create a JSONArray using JSONObject with all values from a database table in java

Viewed 565

I am fetching details from a database table which contains 3 rows in JAVA. I am using JSONarray and JSONObject as follows

JSONObject jsonObject = new JSONObject();
JSONObject mainjsonObject = new JSONObject();
JSONArray ja=new JSONArray();

The data from table is put to the jsonObject as follows for each one:

String qry="select * from details";
ResultSet res = select .executeQuery(qry);
while(res.next){

String Name=res.getString("name");
.
.

jsonObject.put("Name", Name);

.

.

ja.put(jsonObject);

}



mainjsonObject.put("PERSONAL DETAILS",ja);

i should get the output json as follows:

{
"PERSONAL DETAILS": [
    {
      " name": "abc",
      "age": "4",
      "gender": "F",
      "Place": "abc1"
    },
    {
      " name": "xyz",
      "age": "3",
      "gender": "M",
      "Place": "abc2"
    }


]

}

But am getting same values in both like below:

{

"PERSONAL DETAILS": [

    {

     " name": "abc",

     "age": "4",

     "gender": "F",

      "Place": "abc1"

    },

    {

   " name": "abc",

   "age": "4",

   "gender": "F",

   "Place": "abc1"

    }


]

}

Please help me with a solution. I need to get all the values from the tables as an array in JSON format

2 Answers
Related