2014-02-05 33 views
1

我有表“旅行”了一些列的MySQL数据库,它看起来像这样:Java - 如何将数据从mysql数据库转换为JSON数组?

Table Example

每一行中包含的数据为一个行程。我想搜索“asg_id”,并希望使用此“asg_id”获取所有行(行程)。

我的问题是,如何将这个数据与个人旅行tho一个JSON数组? 或者,如何使用此“asg_id”获取所有行程?

回答

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

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

尝试这样的:

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(); 
      } 
} 
相关问题