先处理一下文件:
package com_getjson;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class jsontest {
public static void main(String[] args) {
File file = new File("C:\\Users\\pt0521\\Desktop\\demo(ANSI).json");
File file2 = new File("C:\\Users\\pt0521\\Desktop\\demo2.json");
BufferedReader reader=null;
BufferedWriter writer=null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
writer =new BufferedWriter(new FileWriter(file2));
String tempString = null;
int line = 1;
int flag=0;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
if (tempString.endsWith(",") || tempString.endsWith("{")||tempString.endsWith("[")
||tempString.endsWith("}")||tempString.endsWith("]")||
tempString.contains("Count")){
if(flag==1) {//说明上一条出现了无,结尾
tempString=tempString.replace("\t\t\t\t\t\t\t","");
tempString=tempString.replace("\t\t\t\t\t\t","");
writer.write(tempString+"\n");
flag=0;
}
else {
writer.write(tempString+"\n");
flag=0;
}
}
else {
writer.write(tempString);
flag=1;
}
System.out.println("line " + line + ": " + tempString);
line++;
}
writer.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
}
多数无意,直接上代码,无非就是几层循环:
package com_getjson;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class getjson {
public static void main(String[] args) throws IOException, JSONException {
File file=new File("C:\\Users\\pt0521\\Desktop\\demo2(UTF-8).json");
File file2=new File("C:\\Users\\pt0521\\Desktop\\result_sql.txt");
BufferedWriter writer=null;
writer =new BufferedWriter(new FileWriter(file2));
String content= FileUtils.readFileToString(file,"utf-8");
JSONObject jsonObject=new JSONObject(content);
// System.out.println(content);
// JSONArray reason=jsonObject.getJSONArray("reason");
int error_code=jsonObject.getInt("error_code");
String reason=jsonObject.getString("reason");
JSONArray secondjsonObject=jsonObject.getJSONArray("result");
// System.out.println("第二层json数组的长度"+secondjsonObject.length());
for(int i=0;i<secondjsonObject.length();i++) {
JSONObject thirdjsonObject=secondjsonObject.getJSONObject(i);
int count=thirdjsonObject.getInt("count");
String name=thirdjsonObject.getString("name");
int type_1=thirdjsonObject.getInt("type");
JSONArray forthjsonObject=thirdjsonObject.getJSONArray("list");
System.out.println("第四层的长度"+forthjsonObject.length());
for(int j=0;j<forthjsonObject.length();j++) {
JSONObject fifthjsonObject=forthjsonObject.getJSONObject(j);
int total=fifthjsonObject.getInt("total");
String title_1=fifthjsonObject.getString("title");
String tag=fifthjsonObject.getString("tag");
JSONArray sixthjsonObject=fifthjsonObject.getJSONArray("list");
for(int n=0;n<sixthjsonObject.length();n++) {
JSONObject seventhjsonObject=sixthjsonObject.getJSONObject(n);
int id=seventhjsonObject.getInt("id");
String title_2=seventhjsonObject.getString("title");
String desc=seventhjsonObject.getString("desc");
int type_2=seventhjsonObject.getInt("type");
int riskCount=seventhjsonObject.getInt("riskCount");
reason=reason.replace("'", "''");
name=name.replace("'", "''");
title_1=title_1.replace("'", "''");
tag=tag.replace("'", "''");
title_2=title_2.replace("'", "''");
desc=desc.replace("'", "''");
String result_sql="insert into table_name (erro_code,reason,count_n,name_n,total,title_1,tag,id,title_2,desc,type_1,riskcount,type_2) values(" +
error_code+",'"+reason+"',"+count+",'"+name+"',"+total+",'"+title_1+"','"+tag+"',"+id+",'"+title_2+"','"+desc+"',"+type_1+","+riskCount+","+type_2+");";
writer.write(result_sql+"\n");
System.out.println("呈现的插入语句为");
System.out.println("insert into table_name (erro_code,reason,count_n,name_n,total,title_1,tag,id,title_2,desc,type_1,riskcount,type_2) values(" +
error_code+",'"+reason+"',"+count+",'"+name+"',"+total+",'"+title_1+"','"+tag+"',"+id+",'"+title_2+"','"+desc+"',"+type_1+","+riskCount+","+type_2+");");
}
}
// System.out.println("count的值为"+count);
// System.out.println("name的值为"+name);
// System.out.println("type的值为"+type);
}
System.out.println(error_code);
System.out.println(reason);
writer.close();
}
}