解析mongoDB导出的json文件
使用到了org.json包
import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.LinkedList;import java.util.List;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import com.roiland.mongodb.bean.EcuDictBean;public class importMongoDB { static String path="C:\\Users\\Desktop\\ed.txt"; static String jsonPath="C:\\Users\\Desktop\\edjson.txt"; public static List parseString(String src){List list = new LinkedList();String[] str = src.split("\r\n");for(int i =1;i<str.length;i++){ String[] str1 = str[i].split(","); }return list; } /** * */ public static void getJsonFile(){StringBuffer stringBuffer = new StringBuffer();List list = new LinkedList();String line = null ;try { BufferedReader br = new BufferedReader(new FileReader(new File(jsonPath))); while( (line = br.readLine())!= null ){stringBuffer.append(line);} } catch (FileNotFoundException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();}try { String[] str = stringBuffer.toString().split("}},"); System.out.println(str.length); for(String s:str){JSONObject jsonObject = new JSONObject(s+"}},");JSONArray x = jsonObject.getJSONArray("dv") ;String ei = (String) jsonObject.get("ei");String on = (String) jsonObject.get("on");JSONObject tsJson = (JSONObject) jsonObject.get("ts");//String ts= String.valueOf(tsJson.getLong("$date));long l = tsJson.getLong("$date"); EcuDictBean ed = new EcuDictBean(); List<String> dvlist = new ArrayList<String>(); if(x.length()>0){ for(int i=0;i<x.length();i++){ dvlist.add((String) x.get(i)); } } ed.setDv(dvlist); ed.setEi(ei.toString()); ed.setOn(on.toString()); Date d = new Date(l); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); sdf.format(d); } } catch (JSONException e) { e.printStackTrace();} } public static String getFile(){ StringBuilder sb=new StringBuilder();try { File inFile = new File(path); FileInputStream fi = new FileInputStream(inFile); byte[] b = new byte[fi.available()]; fi.read(b); for(byte c : b){char temp = (char)c;sb.append(temp); } fi.close(); } catch (FileNotFoundException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();}return sb.toString(); } /** * @param args */ public static void main(String[] args) {getJsonFile();//String str =getFile(); }}