我正在尝试使用特定格式的Java将所有表数据写入JSON文件。我喜欢用以下格式编写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"
}]
}]
我已经尝试了太多次并上网但无法获得所需的格式。我的代码完美地将表数据写入JSON文件。
我从数据库表中获取所有数据。这是我的虚拟表的快照。
请检查我的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();
将ResultSet转换为JSON的代码。
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;
}
为此,我使用了json.jar
。以上代码结果是:
[{
"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"
}]
答案 0 :(得分:0)
我不会搞乱jsonArray实现提供的字符串。看起来你自己正在处理一些方括号...... // import java.io.File; // import 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() )
答案 1 :(得分:0)
你需要修改你的SQL&#39;查询。添加ORDEr BY子句以安排的方式来到您的数据。
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;
这里是将所有表数据转换为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;
}
从上面的代码中生成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"
}
]
希望它会对你有所帮助:)。