简体   繁体   English

Java-如何将数据从mysql数据库转换为JSON数组?

[英]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.

相关问题 如何在JAVA中将json数组数据存储到mysql数据库中? - How to store json array data into mysql database in JAVA? 如何从json中的数据库转换数据? - how to convert data from the database in json? 如何使用Hibernate \\ Spring Data将json转换为java对象数据并保存到数据库中? - How to convert from json into java objects data and save into database using Hibernate\Spring Data? 将字符串数据从mysql表转换为java中的json列表 - Convert String data from mysql table to json list in java 如何从服务器获取数据作为json数组并将其转换为Java数组以在android应用程序中使用 - How to get data as an json array from a server and convert it in to java array to use in android application 将树数据结构从数据库转换为Java中的JSON - Convert tree data structure from Database into JSON in Java 将 java 数据列表转换为 JSON 数组数组 - Convert a java list of data into a JSON array of array 如何从数据库(SQL)中读取 Json 并将其转换为 java class? - How to Read a Json from database (SQL) and convert it into a java class? 如何将值从json数组转换为java字符串 - how to convert the values from a json array to a java string 将 sql 数据转换为 Json 数组 [java spark] - Convert sql data to Json Array [java spark]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM