因为工作需要,需要一个职能列表,然后就将猎聘网的职能列表的js文件给下载下来,然而并不能达到用户的需求,原本的4级菜单,全部变成2级菜单选择,但是因为职能类别太多,手动控制太麻烦也容易出错,所有就用json来解析。
完整代码如下:
package isa.qa.boquma.talent;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class TestJson {
public static void main(String[] args) {
TestJson t = new TestJson();
try {
t.jsonTest2();
} catch (JSONException e) {
e.printStackTrace();
}
}
public void jsonTest2() throws JSONException{
FileInputStream fis;
try {
fis = new FileInputStream("C:\\Users\\LENOVO\\files\\job.js");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
FileOutputStream fos = new FileOutputStream("C:\\Users\\LENOVO\\abc.js");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
BufferedReader br = new BufferedReader(isr);
PrintWriter pw = new PrintWriter(osw,true);
String input ;
StringBuffer sb = new StringBuffer();
while((input=br.readLine())!=null){
sb.append(input);
}
JSONObject jsonCategory= new JSONObject(sb.toString()).getJSONObject("category");
JSONObject jsonRelations= new JSONObject(sb.toString()).getJSONObject("relations");
JSONObject jsonList= new JSONObject(sb.toString()).getJSONObject("list");
JSONArray hotjobs = jsonCategory.getJSONArray("hotjobs");
for (int i = 0; i < hotjobs.length(); i++) {
String jobs530=(String) hotjobs.get(i);
JSONArray jsonRelations530 = jsonRelations.getJSONArray(jobs530);
String firstMenu = (String) jsonList.getJSONArray(jobs530).get(0);
System.out.println(jobs530+"======="+firstMenu);
pw.write(jobs530+"======="+firstMenu+"'\n");
for (int j = 0; j < jsonRelations530.length(); j++) {
String two = (String) jsonRelations530.get(j);
JSONArray hotjobs010010 = jsonList.getJSONArray(two);
System.out.println("\""+two+"\":'"+hotjobs010010.get(0)+"'");
pw.write("\""+two+"\":'"+hotjobs010010.get(0)+"'\n");
}
}
//"jobs": [ "cate-01", "cate-02", "cate-03", "cate-04", "cate-05", "cate-06", "cate-07", "cate-08", "cate-09", "cate-10", "cate-11" ]
JSONArray jobs = jsonCategory.getJSONArray("jobs");
for (int i = 0; i < jobs.length(); i++) {
//"cate-03": [ "cate-03-01", "cate-03-02", "cate-03-03", "cate-03-04" ],
String jobsOne=(String) jobs.get(i);
JSONArray jsonRelationsOne = jsonRelations.getJSONArray(jobsOne);
//一级菜单
String firstMenu = (String) jsonList.getJSONArray(jobsOne).get(0);
System.out.println(jobsOne+"======="+firstMenu);
pw.write(jobsOne+"======="+firstMenu+"'\n");
for (int j = 0; j < jsonRelationsOne.length(); j++) {
//"cate-01-01": [ "542", "657", "658" ]
String jobsTwo=(String) jsonRelationsOne.get(j);
JSONArray jsonRelationsTwo = jsonRelations.getJSONArray(jobsTwo);
for (int k = 0; k < jsonRelationsTwo.length(); k++) {
//"658": [ "360320", "360332", "360336", "100350", "350020" ],
String jobsThree=(String) jsonRelationsTwo.get(k);
JSONArray jsonjobsThree = jsonRelations.getJSONArray(jobsThree);
for (int l = 0; l < jsonjobsThree.length(); l++) {
if(jsonjobsThree.get(l)!=null){
String jobsFour=(String) jsonjobsThree.get(l);
JSONArray jsonjobsFour = jsonList.getJSONArray(jobsFour);
pw.write(" \""+jobsFour+"\":'"+jsonjobsFour.get(0)+"'\n");
System.out.println(" \""+jobsFour+"\":'"+jsonjobsFour.get(0)+"'");
}
}
}
}
}
pw.flush();
isr.close();
fis.close();
osw.close();
fos.close();
pw.close();
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}