public classJsonToTxtApplication {public static void main(String[] args) throwsIOException {
SpringApplication.run(JsonToTxtApplication.class, args);
FileInputStream in= new FileInputStream("1.txt");
JsonNode jsonNode= newObjectMapper().readTree(in);/*** 取所有数据并存到HashMap中*/String api;
HashMap> hm = new HashMap<>();
JsonNode node= jsonNode.findValue("paths");
Iterator stringIterator =node.fieldNames();while(stringIterator.hasNext()) {
JsonNode tags= node.findValue((api = stringIterator.next())); //api
Iterator methodsname =tags.fieldNames();while(methodsname.hasNext()) {
String method= methodsname.next(); //方法
JsonNode methods =tags.findValue(method);
String name= methods.findValue("tags").get(0).asText();
String description= methods.findValue("description").asText();
Root root= new Root(name, method, api,description); //当前查询到的一个接口数据//放到hashmap里管理
if(hm.containsKey(root.getName())) {
List roots =hm.get(root.getName());
roots.add(root);
hm.put(root.getName(), roots);
}else{
ArrayList roots = new ArrayList<>();
roots.add(root);
hm.put(root.getName(), roots);
}
}
}/*** 获得name的顺序,并按顺序写入csv*/File file= new File("result.csv");
BufferedWriter bufferedWriter= new BufferedWriter(newOutputStreamWriter(new FileOutputStream(file), "GBK")); //excel不能读取utf-8编码的csv文件
Iterator names = jsonNode.findValue("tags").iterator();while(names.hasNext()) {
String name= names.next().findValue("name").asText();
Iterator iterator1 =hm.get(name).iterator();
bufferedWriter.write(name+ ",");
Boolean isFirst= true;while(iterator1.hasNext()) {
//如果是第一行增加name,如果不是填入空白格if (!isFirst) {
bufferedWriter.write(",");
}else{
isFirst= false;
}
Root next=iterator1.next();
bufferedWriter.write(next.getMethod()+ "," +next.getApi()+ "," +next.getDescription());
bufferedWriter.newLine();
}
}
bufferedWriter.close();
//打开生成的csv文件
Runtime.getRuntime().exec("cmd /c start F:/Project/JsonSoup/result.csv");
System.out.println("done");
}}