java解json

先处理一下文件:

package com_getjson;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class jsontest {
	public static void main(String[] args) {
	    File file = new File("C:\\Users\\pt0521\\Desktop\\demo(ANSI).json");
	    File file2 = new File("C:\\Users\\pt0521\\Desktop\\demo2.json");
	    BufferedReader reader=null;
	    BufferedWriter writer=null;
	    try {
	        System.out.println("以行为单位读取文件内容,一次读一整行:");
	        reader = new BufferedReader(new FileReader(file));
	        writer =new BufferedWriter(new FileWriter(file2));
	        String tempString = null;
	        int line = 1;
	        int flag=0;
	        // 一次读入一行,直到读入null为文件结束
	        while ((tempString = reader.readLine()) != null) {
	            // 显示行号
	        	if (tempString.endsWith(",") || tempString.endsWith("{")||tempString.endsWith("[")
	        			||tempString.endsWith("}")||tempString.endsWith("]")||
	        			tempString.contains("Count")){
	        		if(flag==1) {//说明上一条出现了无,结尾
	        			tempString=tempString.replace("\t\t\t\t\t\t\t","");
	        			tempString=tempString.replace("\t\t\t\t\t\t","");
	        			writer.write(tempString+"\n");
	        			flag=0;
	        		}
	        		else {
	        			writer.write(tempString+"\n");
	        			flag=0;
	        		}
	        	}
	        	else {
	        		writer.write(tempString);
	        		flag=1;
	        	}
	            System.out.println("line " + line + ": " + tempString);
	            line++;
	        }
	        writer.close();
	        reader.close();
	    } catch (IOException e) {
	        e.printStackTrace();
	    } finally {
	        if (reader != null) {
	            try {
	                reader.close();
	            } catch (IOException e1) {
	            }
	        }
	    }
	}

}

 

多数无意,直接上代码,无非就是几层循环:
 

package com_getjson;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class getjson {
	public static void main(String[] args) throws IOException, JSONException {
        File file=new File("C:\\Users\\pt0521\\Desktop\\demo2(UTF-8).json");
        File file2=new File("C:\\Users\\pt0521\\Desktop\\result_sql.txt");
        BufferedWriter writer=null;
        writer =new BufferedWriter(new FileWriter(file2));
        String content= FileUtils.readFileToString(file,"utf-8");
        JSONObject jsonObject=new JSONObject(content);
//        System.out.println(content);
//        JSONArray reason=jsonObject.getJSONArray("reason");
        int error_code=jsonObject.getInt("error_code");
        String reason=jsonObject.getString("reason");
        JSONArray secondjsonObject=jsonObject.getJSONArray("result");
//        System.out.println("第二层json数组的长度"+secondjsonObject.length());
        for(int i=0;i<secondjsonObject.length();i++) {
        	JSONObject thirdjsonObject=secondjsonObject.getJSONObject(i);
        	int count=thirdjsonObject.getInt("count");
        	String name=thirdjsonObject.getString("name");
        	int type_1=thirdjsonObject.getInt("type");
        	JSONArray forthjsonObject=thirdjsonObject.getJSONArray("list");
        	System.out.println("第四层的长度"+forthjsonObject.length());
        	for(int j=0;j<forthjsonObject.length();j++) {
        		JSONObject fifthjsonObject=forthjsonObject.getJSONObject(j);
        		int total=fifthjsonObject.getInt("total");
        		String title_1=fifthjsonObject.getString("title");
        		String tag=fifthjsonObject.getString("tag");
        		JSONArray sixthjsonObject=fifthjsonObject.getJSONArray("list");
        		for(int n=0;n<sixthjsonObject.length();n++) {
        			JSONObject seventhjsonObject=sixthjsonObject.getJSONObject(n);
        			int id=seventhjsonObject.getInt("id");
        			String title_2=seventhjsonObject.getString("title");
        			String desc=seventhjsonObject.getString("desc");
        			int type_2=seventhjsonObject.getInt("type");
        			int riskCount=seventhjsonObject.getInt("riskCount");
        			reason=reason.replace("'", "''");
        			name=name.replace("'", "''");
        			title_1=title_1.replace("'", "''");
        			tag=tag.replace("'", "''");
        			title_2=title_2.replace("'", "''");
        			desc=desc.replace("'", "''");
        			String result_sql="insert into table_name (erro_code,reason,count_n,name_n,total,title_1,tag,id,title_2,desc,type_1,riskcount,type_2) values(" +
                			error_code+",'"+reason+"',"+count+",'"+name+"',"+total+",'"+title_1+"','"+tag+"',"+id+",'"+title_2+"','"+desc+"',"+type_1+","+riskCount+","+type_2+");";
        			writer.write(result_sql+"\n");
        			System.out.println("呈现的插入语句为");
        			System.out.println("insert into table_name (erro_code,reason,count_n,name_n,total,title_1,tag,id,title_2,desc,type_1,riskcount,type_2) values(" +
                			error_code+",'"+reason+"',"+count+",'"+name+"',"+total+",'"+title_1+"','"+tag+"',"+id+",'"+title_2+"','"+desc+"',"+type_1+","+riskCount+","+type_2+");");
        		}
        	}
//        	System.out.println("count的值为"+count);
//        	System.out.println("name的值为"+name);
//        	System.out.println("type的值为"+type);
        }
        System.out.println(error_code);
        System.out.println(reason);
        writer.close();
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值