Json数组是括在方括号内的值的有序集合,即,它以'['开始,以']'结尾。数组中的值以','(逗号)分隔。
{ "books": [ Java, JavaFX, Hbase, Cassandra, WebGL, JOGL] }
json-simple是一个轻量级的库,用于处理JSON对象。使用此程序,您可以使用Java程序读取或写入JSON文档的内容。
以下是JSON简单库的maven依赖关系-
<dependencies> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> </dependencies>
将此粘贴到pom.xml文件末尾的<dependencies> </ dependencies>标记内。(在</ project>标记之前)
让我们使用CREATE语句在MySQL数据库中创建一个名为MyPlayers的表,如下所示-
CREATE TABLE MyPlayers( ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255), PRIMARY KEY (ID) );
现在,我们将使用INSERT语句在MyPlayers表中插入5条记录-
insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India'); insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica'); insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka'); insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India'); insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India'); insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India'); insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');
使用JDBC将上面创建的MyPlayers表的内容读取到JSON文件中-
使用DriverManager类的registerDriver()方法或forName()
名为Class的类的方法注册所需数据库的Driver类。
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
通过将数据库的URL,数据库中用户的用户名和密码(字符串格式)作为参数传递给DriverManager类的getConnection()方法来创建连接对象。
Connection mysqlCon = DriverManager.getConnection(mysqlUrl, "root", "password");
使用连接接口的createStatement()方法创建一个Statement对象。
Statement stmt = con.createStatement();
使用该executeQuery()
方法执行SELECT查询以将MyPlayers表的内容检索到ResultSet对象中。
String query = "Select * from MyPlayers"; ResultSet rs = stmt.executeQuery(query);
通过实例化JSON简单库的JSONObject类,创建JSON文档(以表示MyPlayers表)。
//创建一个JSONObject对象 JSONObject jsonObject = new JSONObject();
通过实例化JSONArray类,创建一个JSON数组来保存MyPlayers表的记录。
JSONArray array = new JSONArray();
对于MyPLayers表中的每个记录,创建一个JSON对象(再次),使用put()方法将记录的内容(从ResultSet对象获得)插入到其中。
最后,将JSON对象添加到上一步中创建的数组中。
while(rs.next()) { JSONObject record = new JSONObject(); //将键值对插入json对象 record.put("ID", rs.getInt("ID")); record.put("First_Name", rs.getString("First_Name")); record.put("Last_Name", rs.getString("Last_Name")); record.put("Date_Of_Birth", rs.getDate("Date_Of_Birth")); record.put("Place_Of_Birth", rs.getString("Place_Of_Birth")); record.put("Country", rs.getString("Country")); array.add(record); }
添加JSON数组的所有必需记录后,使用put()方法将其添加到父JSON对象。
jsonObject.put("Players_data", array);
使用FileWriter类将创建的JSON对象写入文件-
FileWriter file = new FileWriter("E:/json_array_output.json"); file.write(jsonObject.toJSONString()); file.close();
以下JDBC程序将MyPlayers表的内容读取到JSON文件中。
import java.io.FileWriter; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import org.json.simple.JSONArray; import org.json.simple.JSONObject; public class DataBaseToJson { public static ResultSet RetrieveData() throws Exception { //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //获得连接 String mysqlUrl = "jdbc:mysql://localhost/sample_database"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //创建语句 Statement stmt = con.createStatement(); //检索记录 ResultSet rs = stmt.executeQuery("Select * from MyPlayers"); return rs; } public static void main(String args[]) throws Exception { //创建一个JSONObject对象 JSONObject jsonObject = new JSONObject(); //创建一个json数组 JSONArray array = new JSONArray(); ResultSet rs = RetrieveData(); //将ResutlSet数据插入json对象 while(rs.next()) { JSONObject record = new JSONObject(); //将键值对插入json对象 record.put("ID", rs.getInt("ID")); record.put("First_Name", rs.getString("First_Name")); record.put("Last_Name", rs.getString("Last_Name")); record.put("Date_Of_Birth", rs.getDate("Date_Of_Birth")); record.put("Place_Of_Birth", rs.getString("Place_Of_Birth")); record.put("Country", rs.getString("Country")); array.add(record); } jsonObject.put("Players_data", array); try { FileWriter file = new FileWriter("E:/output.json"); file.write(jsonObject.toJSONString()); file.close(); } catch (IOException e) { //TODO自动生成的捕获块 e.printStackTrace(); } System.out.println("JSON file created......); } }
输出结果
JSON file created......
如果您观察到output.json文件,则可以看到读取的内容为-
{ "Players_data": [ { "First_Name":"Shikhar", "Place_Of_Birth":"Delhi", "Last_Name":"Dhawan", "Country":"India", "ID":1, "Date_Of_Birth":1981-12-05 }, { "First_Name":"Jonathan", "Place_Of_Birth":"CapeTown", "Last_Name":"Trott", "Country":"SouthAfrica", "ID":2, "Date_Of_Birth":1981-04-22 }, { "First_Name":"Kumara", "Place_Of_Birth":"Matale", "Last_Name":"Sangakkara", "Country":"Srilanka", "ID":3, "Date_Of_Birth":1977-10-27 }, { "First_Name":"Virat", "Place_Of_Birth":"Mumbai", "Last_Name":"Kohli", "Country":"India", "ID":4, "Date_Of_Birth":1988-11-05 }, { "First_Name":"Rohit", "Place_Of_Birth":"Nagpur", "Last_Name":"Sharma", "Country":"India", "ID":5, "Date_Of_Birth":1987-04-30 }, { "First_Name":"Ravindra", "Place_Of_Birth":"Nagpur", "Last_Name":"Jadeja", "Country":"India", "ID":6, "Date_Of_Birth":1988-12-06 }, { "First_Name":"James", "Place_Of_Birth":"Burnely", "Last_Name":"Anderson", "Country":"England", "ID":7, "Date_Of_Birth":1982-06-30 }, { "First_Name":"Ryan", "Place_Of_Birth":"South Africa", "Last_Name":"McLaren", "Country":"India", "ID":8, "Date_Of_Birth":1983-02-09 } ] }