将文本内容解析成json格式

因为工作需要,需要一个职能列表,然后就将猎聘网的职能列表的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();
        }

    }

}
已标记关键词 清除标记
相关推荐
全国省市县三级行政单位的JSON格式文本文件. 通过解析该文件可以: 1、将全国JSON格式文本文件IO读取为字符串。 2、将该字符串进行JSON解析,加载到缓存中。 3、依据省名称,可以查找到该省下的所有城市。 4、依据市名称,可以查找到该市下的所有区县。 5、依据市名称查找该市属于哪个省。 以江苏省为例如下: "江苏省":{"南京市":["玄武区","白下区","秦淮区","建邺区","鼓楼区","下关区","浦口区","栖霞区","雨花台区","江宁区","六合区","溧水县","高淳县"],"无锡市":["崇安区","南长区","北塘区","锡山区","惠山区","滨湖区","江阴市","宜兴市"],"徐州市":["鼓楼区","云龙区","九里区","贾汪区","泉山区","丰县","沛县","铜山县","睢宁县","新沂市","邳州市"],"常州市":["天宁区","钟楼区","戚墅堰区","新北区","武进区","溧阳市","金坛市"],"苏州市":["沧浪区","平江区","金阊区","虎丘区","吴中区","相城区","常熟市","张家港市","昆山市","吴江市","太仓市"],"南通市":["崇川区","港闸区","海安县","如东县","启东市","如皋市","通州市","海门市"],"连云港市":["连云区","新浦区","海州区","赣榆县","东海县","灌云县","灌南县"],"淮安市":["清河区","楚州区","淮阴区","清浦区","涟水县","洪泽县","盱眙县","金湖县"],"盐城市":["亭湖区","盐都区","响水县","滨海县","阜宁县","射阳县","建湖县","东台市","大丰市"],"扬州市":["广陵区","邗江区","郊区","宝应县","仪征市","高邮市","江都市"],"镇江市":["京口区","润州区","丹徒区","丹阳市","扬中市","句容市"],"泰州市":["海陵区","高港区","兴化市","靖江市","泰兴市","姜堰市"],"宿迁市":["宿城区","宿豫区","沭阳县","泗阳县","泗洪县"]}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页