[英]How to write table data to JSON file in Java?
I'm trying to write all table data into a JSON file using Java in a particular format. 我正在尝试使用Java以特定格式将所有表数据写入JSON文件。 I like to get the JSON file written in the following format:
我喜欢以以下格式编写JSON文件:
[{
"com_name": "Google",
"com_code": 12,
"IT": [{
"type_3": "Hero",
"type_2": "To",
"type_1": "Zero",
"cat_no": "AC06",
"cat_desc": "CareWorld"
}, {
"type_3": "Hero",
"type_2": "To",
"type_1": "Zero",
"cat_no": "AC06",
"cat_desc": "CareWorld"
}],
"SALES": [{
"type_3": "",
"type_2": "",
"type_1": "",
"cat_no": "SL01",
"cat_desc": "SellBetter"
}],
"MARKETING": [{
"type_3": "OK",
"type_2": "OK",
"type_1": "AZC",
"cat_no": "M1",
"cat_desc": "Required"
}]
}, {
"com_name": "Microsoft",
"com_code": 18,
"PRODUCT": [{
"type_3": "INDIA",
"type_2": "JAPAN",
"type_1": "USA",
"cat_no": "P01",
"cat_desc": "Windows10"
}]
}, {
"com_name": "StackOverflow",
"com_code": 14,
"IT": [{
"type_3": "JS",
"type_2": "JSON",
"type_1": "Java",
"cat_no": "QA",
"cat_desc": "QuestionandAnswer"
}],
"SALES": [{
"type_3": "",
"type_2": "DONE",
"type_1": "",
"cat_no": "S1",
"cat_desc": "internet"
}]
}]
I have tried it too many times and surfing the internet but unable to get required format. 我已经尝试了太多次,无法上网,但是无法获得所需的格式。 My code is perfectly writing table data into the JSON file.
我的代码完美地将表数据写入JSON文件。
I am fetching all data from the database table. 我正在从数据库表中获取所有数据。 Here is the snapshot of my dummy table.
这是我的虚拟表的快照。
Please check what's wrong with my Java code. 请检查我的Java代码有什么问题。
WriteJsonFile.java
ResultSet res = stmt.executeQuery("SELECT com_name,com_code,dept,cat_no,cat_desc,type_1,type_2,type_3 FROM json_data");
JSONArray jsonArray = new JsonConverter().convertToJSON(res);
FileWriter fileWriter = new FileWriter("json_file.json");
JSONObject obj = null;
fileWriter.write("[");
for (int i = 0; i < jsonArray.length(); i++) {
obj = (JSONObject) jsonArray.get(i);
if (i != jsonArray.length() - 1) {
fileWriter.write(obj.toString() + ",\n");
} else {
fileWriter.write(obj.toString());
}
}
fileWriter.write("]");
fileWriter.flush();
And the code to convert ResultSet to JSON. 以及将ResultSet转换为JSON的代码。
JsonConverter.java JsonConverter.java
public static JSONArray convertToJSON(ResultSet resultSet) throws Exception {
JSONArray jsonArray = new JSONArray();
while (resultSet.next()) {
JSONObject obj = null;
obj = new JSONObject();
int total_rows = resultSet.getMetaData().getColumnCount();
for (int i = 0; i < total_rows; i++) {
obj.put(resultSet.getMetaData().getColumnLabel(i + 1).toLowerCase(), resultSet.getObject(i + 1) != null ? resultSet.getObject(i + 1) : "");
}
jsonArray.put(obj); }
return jsonArray;
}
To do this I have used the json.jar
. 为此,我使用了
json.jar
。 And above code result is: 上面的代码结果是:
[{
"type_3": "Hero",
"type_2": "To",
"type_1": "Zero",
"cat_no": "AC06",
"cat_desc": "CareWorld",
"com_code": 12,
"dept": "IT",
"com_name": "Google"
}, {
"type_3": "Hero",
"type_2": "To",
"type_1": "Zero",
"cat_no": "AC06",
"cat_desc": "CareWorld",
"com_code": 12,
"dept": "IT",
"com_name": "Google"
}, {
"type_3": "",
"type_2": "",
"type_1": "",
"cat_no": "SL01",
"cat_desc": "SellBetter",
"com_code": 12,
"dept": "SALES",
"com_name": "Google"
}, {
"type_3": "OK",
"type_2": "OK",
"type_1": "AZC",
"cat_no": "M1",
"cat_desc": "Required",
"com_code": 12,
"dept": "MARKETING",
"com_name": "Google"
}, {
"type_3": "JS",
"type_2": "JSON",
"type_1": "Java",
"cat_no": "QA",
"cat_desc": "QuestionandAnswer",
"com_code": 14,
"dept": "IT",
"com_name": "StackOverflow"
}, {
"type_3": "INDIA",
"type_2": "JAPAN",
"type_1": "USA",
"cat_no": "P01",
"cat_desc": "Windows10",
"com_code": 18,
"dept": "PRODUCT",
"com_name": "Microsoft"
}, {
"type_3": "",
"type_2": "DONE",
"type_1": "",
"cat_no": "S1",
"cat_desc": "internet",
"com_code": 14,
"dept": "SALES",
"com_name": "StackOverflow"
}]
I wouldn't mess with the string provided by the jsonArray implementation. 我不会弄混jsonArray实现提供的字符串。 Looks like you are handling some square brackets yourself... //import java.io.File;
看起来您正在自己处理一些方括号... // import java.io.File; //import java.io.FileOutputStream;
//导入java.io.FileOutputStream;
private boolean writeFile(File file,String conts){
try {
file.createNewFile();
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(conts);
myOutWriter.close();
fOut.flush();
fOut.close();
return(true);
}catch(IOException e){
return(false);
}
}
ResultSet res = stmt.executeQuery("SELECT com_name,com_code,dept,cat_no,cat_desc,type_1,type_2,type_3 FROM json_data");
JSONArray jsonArray = new JsonConverter().convertToJSON(res);
writeFile(new File(".json_file.json"),jsonArray.toString() )
You need to modify your 'SQL' query. 您需要修改“ SQL”查询。 Add the ORDEr BY clause to come to your data in the arranged way.
添加ORDEr BY子句以安排的方式将其引入您的数据。
SQL: SQL:
SELECT com_name,com_code,dept,cat_no,cat_desc,type_1,type_2,type_3 FROM json_data order by com_name,dept;
And here the method to convert all table data to JSON. 这里是将所有表数据转换为JSON的方法。
public static JSONArray convert(ResultSet rs) {
Set<String> nameSet = new HashSet<>();
Set<String> deptSet = new HashSet<>();
JSONArray jsonArray = new JSONArray();
JSONArray jsonArray2 = null;
JSONObject obj = null;
JSONObject obj2 = null;
try {
while (rs.next()) {
int total_rows = rs.getMetaData().getColumnCount();
if (nameSet.contains(rs.getString(1))) {
if (deptSet.contains(rs.getString(3))) {
obj2 = new JSONObject();
for (int j = 3; j < total_rows; j++) {
obj2.put(rs.getMetaData().getColumnLabel(j + 1).toLowerCase(),
rs.getObject(j + 1) != null ? rs.getObject(j + 1) : "");
}
jsonArray2.put(obj2);
} else {
obj2 = new JSONObject();
jsonArray2 = new JSONArray();
deptSet.add(rs.getString(3));
for (int j = 3; j < total_rows; j++) {
obj2.put(rs.getMetaData().getColumnLabel(j + 1).toLowerCase(),
rs.getObject(j + 1) != null ? rs.getObject(j + 1) : "");
}
jsonArray2.put(obj2);
obj.put(rs.getString(3), jsonArray2);
}
} else {
if (obj != null) {
jsonArray.put(obj);
}
deptSet.removeAll(deptSet);
obj = new JSONObject();
obj2 = new JSONObject();
jsonArray2 = new JSONArray();
nameSet.add(rs.getString(1));
for (int i = 0; i < 2; i++) {
obj.put(rs.getMetaData().getColumnLabel(i + 1).toLowerCase(),
rs.getObject(i + 1) != null ? rs.getObject(i + 1) : "");
}
if (deptSet.contains(rs.getString(3))) {
} else {
deptSet.add(rs.getString(3));
for (int j = 3; j < total_rows; j++) {
obj2.put(rs.getMetaData().getColumnLabel(j + 1).toLowerCase(),
rs.getObject(j + 1) != null ? rs.getObject(j + 1) : "");
}
}
jsonArray2.put(obj2);
obj.put(rs.getString(3), jsonArray2);
}
}
jsonArray.put(obj);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Final JSON: " + jsonArray);
return jsonArray;
}
Generated JSON from the above code. 从上面的代码生成JSON。
[
{
"MARKETING": [
{
"type_3": "OK",
"type_2": "OK",
"type_1": "AZC",
"cat_no": "M1",
"cat_desc": "Required"
}
],
"com_code": 12,
"IT": [
{
"type_3": "",
"type_2": "",
"type_1": "a",
"cat_no": "XYZ",
"cat_desc": "World"
},
{
"type_3": "",
"type_2": "fine",
"type_1": "Right",
"cat_no": "ABC",
"cat_desc": "People"
}
],
"SALES": [
{
"type_3": "",
"type_2": "",
"type_1": "",
"cat_no": "SL01",
"cat_desc": "Sell Better"
}
],
"com_name": "Google"
},
{
"PRODUCT": [
{
"type_3": "INDIA",
"type_2": "JAPAN",
"type_1": "USA",
"cat_no": "P01",
"cat_desc": "Windows 10"
}
],
"com_code": 18,
"com_name": "Microsoft"
},
{
"com_code": 14,
"IT": [
{
"type_3": "JS",
"type_2": "JSON",
"type_1": "Java",
"cat_no": "QA",
"cat_desc": "Question and Answer"
}
],
"SALES": [
{
"type_3": "",
"type_2": "DONE",
"type_1": "",
"cat_no": "S1",
"cat_desc": "internet"
}
],
"com_name": "StackOverflow"
}
]
Hope it will help you :) 希望它能对您有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.