如何在 JSON 响应中的 2 个对象之间添加换行符?还有如何从 json 文件中删除 [ ] 方括号?
How to add newline between the 2 objects in JSON response ? And also how to remove the [ ] square brackets from the json file?
我也尝试使用 line.separator 和 \n。但它不起作用。
这是我的代码:
ResultSetMetaData rsmd = rs.getMetaData();
JSONArray json = new JSONArray();
while (rs.next()) {
int numColumns = rsmd.getColumnCount();
JSONObject obj = new JSONObject();
for (int i = 1; i <= numColumns; i++) {
String column_name = rsmd.getColumnName(i);
obj.put(column_name, rs.getObject(column_name));
}
json.put(obj);
}
PrintWriter file = new PrintWriter("JFile.json");
file.println(json);
file.close();
} catch (SQLException | FileNotFoundException e) {
e.printStackTrace();
}
编辑 1
这是更新后的 JSONObject 代码,还在对象中添加缩进因子显示 com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input: expected close marker for Object:
ResultSetMetaData rsmd = rs.getMetaData();
JSONObject obj = new JSONObject();
while (rs.next()) {
int numColumns = rsmd.getColumnCount();
for (int i = 1; i <= numColumns; i++) {
String column_name = rsmd.getColumnName(i);
obj.put(column_name, rs.getObject(column_name));
}
}
PrintWriter file = new PrintWriter("downloads/JSONFile.json");
file.println(obj.toString());
file.close();
} catch (SQLException | FileNotFoundException e) {
e.printStackTrace();
}
ResultSetMetaData rsmd = rs.getMetaData();
JSONArray jsonArr = new JSONArray();
while (rs.next()) {
int numColumns = rsmd.getColumnCount();
JSONObject obj = new JSONObject();
for (int i = 1; i <= numColumns; i++) {
String column_name = rsmd.getColumnName(i);
obj.put(column_name, rs.getObject(column_name));
}
jsonArr.put(obj);
}
//add this JSONArray to another JSONObject
JSONObject rootJSONObject = new JSONObject();
rootJSONObject.put("data",jsonArr);
PrintWriter printWriter = new PrintWriter("sample.json");
printWriter.write(rootJSONObject.toString(4).replace("{", "\n{\n").replace("}", "\n}\n"));
printWriter.close();
我也尝试使用 line.separator 和 \n。但它不起作用。 这是我的代码:
ResultSetMetaData rsmd = rs.getMetaData();
JSONArray json = new JSONArray();
while (rs.next()) {
int numColumns = rsmd.getColumnCount();
JSONObject obj = new JSONObject();
for (int i = 1; i <= numColumns; i++) {
String column_name = rsmd.getColumnName(i);
obj.put(column_name, rs.getObject(column_name));
}
json.put(obj);
}
PrintWriter file = new PrintWriter("JFile.json");
file.println(json);
file.close();
} catch (SQLException | FileNotFoundException e) {
e.printStackTrace();
}
编辑 1 这是更新后的 JSONObject 代码,还在对象中添加缩进因子显示 com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input: expected close marker for Object:
ResultSetMetaData rsmd = rs.getMetaData();
JSONObject obj = new JSONObject();
while (rs.next()) {
int numColumns = rsmd.getColumnCount();
for (int i = 1; i <= numColumns; i++) {
String column_name = rsmd.getColumnName(i);
obj.put(column_name, rs.getObject(column_name));
}
}
PrintWriter file = new PrintWriter("downloads/JSONFile.json");
file.println(obj.toString());
file.close();
} catch (SQLException | FileNotFoundException e) {
e.printStackTrace();
}
ResultSetMetaData rsmd = rs.getMetaData();
JSONArray jsonArr = new JSONArray();
while (rs.next()) {
int numColumns = rsmd.getColumnCount();
JSONObject obj = new JSONObject();
for (int i = 1; i <= numColumns; i++) {
String column_name = rsmd.getColumnName(i);
obj.put(column_name, rs.getObject(column_name));
}
jsonArr.put(obj);
}
//add this JSONArray to another JSONObject
JSONObject rootJSONObject = new JSONObject();
rootJSONObject.put("data",jsonArr);
PrintWriter printWriter = new PrintWriter("sample.json");
printWriter.write(rootJSONObject.toString(4).replace("{", "\n{\n").replace("}", "\n}\n"));
printWriter.close();