[英]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: 我有一个带有表“ trips”和一些列的mysql数据库,它看起来像这样:
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" .
我想搜索“ asg_id”,并希望获得所有带有“ asg_id”的行(行程)。
My question is, how to convert this data with the individual trips tho a JSON Array? 我的问题是,如何通过JSON数组的单个行程转换此数据? Or, how can I get all trips with this "asg_id"?
或者,如何使用此“ asg_id”获得所有旅程?
JSONObject json = new JSONObject();
json .put("json", collection/object);
request.setAttribute("jsonObject", json.toString());
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.