关闭

json文件生成与解析

340人阅读 评论(0) 收藏 举报
分类:

生成:

 <span style="white-space:pre">	</span>    count = count + 1;
            Gson gson = new Gson();
            String jsonStr = gson.toJson(maps);
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            String filename = (count) + "-" + dateFormat.format(now).toString();

            JsonFile("D:/baixinpaCong/全国/" + work, filename, jsonStr);

public static void JsonFile(String path, String FileName, String Message) {
        System.out.println("**message:" + Message);

        FileWriter fw = null;
        File f = new File(path + "/" + FileName);

        try {
            File destPar = new File(path);

            if (!destPar.exists()) {
                destPar.mkdirs();
            }

            if (!f.exists()) {
                f.createNewFile();
            }
            fw = new FileWriter(f);
            BufferedWriter out = new BufferedWriter(fw);
            out.write(Message);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("end,next...");
        System.out.println(" ");


    }
解析:


File[] files = file.listFiles();
for (int k = 0; k < fileNumber; k++) {
    insertInfo(files[k]);
}

 public void insertInfo(File file) {
        try {
            if (file.isFile() && file.exists()) { //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file));
                BufferedReader bufferedReader = new BufferedReader(read);
                StringBuffer content = new StringBuffer();
                String lineTxt = null;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    content.append(lineTxt);
                }
                read.close();

//                System.out.println(content);
                //TODO 开始往解析并插入数据数据

                JSONObject jsonObject = new JSONObject(content.toString());

                Map result = new HashMap();
                Iterator iterator = jsonObject.keys();
                String key = null;
                Object value = null;

                while (iterator.hasNext()) {

                    key = (String) iterator.next();
                    value = jsonObject.get(key);

                    result.put(key, value);

                }

                WorkPaCong wp = new WorkPaCong();


                String wages = result.get("wages").toString();


MVN:
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20160810</version>
</dependency>



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:66306次
    • 积分:2172
    • 等级:
    • 排名:第18233名
    • 原创:80篇
    • 转载:19篇
    • 译文:0篇
    • 评论:17条