Skip to content Skip to sidebar Skip to footer

Java - How To Convert Data From Mysql Database To A Json Array?

I have a mysql database with the table 'trips' with some columns, it looks like this: Each row contains the data for one trip. And I want to search for the 'asg_id' and want to ge

Solution 1:

JSONObject json = new JSONObject();
json .put("json", collection/object);

request.setAttribute("jsonObject", json.toString());

Solution 2:

Try like this:

Cursor c= dbo.rawQuery("SELECT * from trips where asg_id="
                    + id, null);
JSONObject jsonObject = new JSONObject();
JSONObject jsonObjectInner = new JSONObject();   

 if (c!= null && c.getCount() > 0) {

 c.moveToFirst();

try {
do{

jsonObjectInner.put("trip_id", c.getString(c.getColumnIndex("trip_id"));
jsonObjectInner.put("asg_id", c.getString(c.getColumnIndex("asg_id"));
jsonObjectInner.put("date_start", c.getString(c.getColumnIndex("date_start"));
jsonObjectInner.put("time_start",c.getString(c.getColumnIndex("time_start"));
jsonObjectInner.put("time_stop", c.getString(c.getColumnIndex("time_stop"));
jsonObject.put("trip", jsonObjectInner);
 }while(c.moveToNext());

}
catch (Exception e) {
        e.printStackTrace();
          }
}

Post a Comment for "Java - How To Convert Data From Mysql Database To A Json Array?"