1

I have a mysql database with the table "trips" with some columns, it looks like this:

Table Example

Each row contains the data for one trip. And I want to search for the "asg_id" and want to get all rows(trips) with this "asg_id" .

My question is, how to convert this data with the individual trips tho a JSON Array? Or, how can I get all trips with this "asg_id"?

2 Answers 2

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

request.setAttribute("jsonObject", json.toString());
Sign up to request clarification or add additional context in comments.

Comments

0

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();
          }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.