解析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();

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值