注意:json读取已支持中文,csv写出用ultraedit打开中文没问题,但如要在excel中支持中文,要另行把csv file另存为 ascii 格式。
package utils;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
public class ReadJson {
final static String CO = ",";
final static String BR = "\r\n";
final static String READPATH = "D:/personal/knowledge/dataMining/dataset/castle/testdata/practice_10000.json";
final static String WRITEPATH = "D:/personal/knowledge/dataMining/dataset/castle/testdata/practice_10000.csv";
public static void main(String[] args) {
List<Resume> resumeList = new ArrayList<Resume>();
List<String> dataList = ReadFile(READPATH);
try {
Iterator<String> itr = dataList .iterator();
while(itr.hasNext()) {
Resume resume = new Resume();
List<Exp> expList = new ArrayList<Exp>();
String data = (String) itr.next();
JSONObject jsonObj = new JSONObject(data);
// System.out.println(jsonObj);
resume.setOid(jsonObj.getJSONObject("_id").getString("$oid"));
resume.setAge(jsonObj.getString("age"));
resume.setDegree(Integer.parseInt(jsonObj.getString("degree")));
resume.setGender(jsonObj.getString("gender"));
resume.setId(jsonObj.getString("id"));
resume.setMajor(jsonObj.getString("major"));
JSONArray expJsonList = jsonObj.getJSONArray("workExperienceList");
for (int i=0; i < expJsonList.length(); i++) {
JSONObject expJsonObj = expJsonList.getJSONObject(i);
Exp exp = new Exp();
exp.setDepartment(expJsonObj.getString("department"));
exp.setEnd_date(expJsonObj.getString("end_date"));
exp.setIndustry(expJsonObj.getString("industry"));
exp.setPosition_name(expJsonObj.getString("position_name"));
exp.setSalary(Integer.parseInt(expJsonObj.getString("salary")));
exp.setSize(Integer.parseInt(expJsonObj.getString("size")));
exp.setStart_date(expJsonObj.getString("start_date"));
exp.setType(expJsonObj.getString("type"));
expList.add(exp);
}
resume.setExp(expList);
resumeList.add(resume);
}
System.out.println("resume size:" + resumeList.size());
writeCsv(resumeList, WRITEPATH);
} catch (Exception e) {
e.printStackTr