json解析

5 篇文章 0 订阅

380万条的json格式的数据进行解析

数据格式如下:

{
  "_id" : "b454e650cb1a4fa4a2f2bd6899fbfa73",
  "bb" : "116.2451019,39.8486099,116.2451019,39.8486099",
  "pts" : [{
      "1" : 0,
      "2" : 39.8486099,
      "3" : 116.2451019,
      "4" : 0.0,
      "5" : 0.0,
      "6" : "2012-03-07 14:25:08",
      "7" : "2012-03-07 14:25:10",
      "8" : "2",
      "9" : 0.0,
      "10" : 1
    }, {
      "1" : 1,
      "2" : 39.8486099,
      "3" : 116.2451019,
      "4" : 0.0,
      "5" : 0.0,
      "6" : "2012-03-07 14:25:16",
      "7" : "2012-03-07 14:25:17",
      "8" : "1",
      "9" : 0.0,
      "10" : 1
    }, {
      "1" : 2,
      "2" : 39.8486099,
      "3" : 116.2451019,
      "4" : -1.0,
      "5" : 0.0,
      "6" : "2012-03-07 14:25:18",
      "7" : "2012-03-07 14:25:20",
      "8" : "1",
      "9" : 0.0,
      "10" : 1
    }]
}


获取“_id”值和“bb”属性值(两点的经纬度坐标),获取“pts”属性中第一个标签6的值(起始时间)和最后一个标签7的属性值(终止时间),并根据经纬度计算两点之间的距离,根据起始时间和终止时间计算其时间差,得到每个“_id”的对应的距离和时间差

private final double PI = 3.1415926; //圆周率
	private final static double earthRadius = 6371; //地球半径,单位为KM
	
	public static void parseJson(BufferedReader bufferedReader) 
			throws IOException, ParseException, java.text.ParseException{
		
		String line = ""; //一行一行的形式读取文件
		String[] pts6FirstTime =null;//第一个坐标点标签6的时间
		String pts6Time ="";
		String ptsTime7 = "";
		String reslut = ""; //计算最终结果
		double betweenTime = 0.0d;//轨迹点之间的时间差,以小时(h)为单位
		
		//longitude1、longitude2、dimension1、dimension2分别为经度坐标和维度坐标
		double longitude1 = 0.0d;
		double dimension1 = 0.0d;
		
		double longitude2 = 0.0d;
		double dimension2 = 0.0d; 
		
		//设置时间格式
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		//创建json解析对象
		JSONParser jsonParser = new JSONParser();
		
		//创建一个distanceAndTime对象
		DistanceAndTime distanceAndTime = new DistanceAndTime();
		
		while((line = bufferedReader.readLine())!=null){
			//开始解析
			JSONObject jsonObject = (JSONObject)jsonParser.parse(line);
			
			String _id = (String) jsonObject.get("_id");//得到轨迹的id号
			
			String bb = (String) jsonObject.get("bb");//得到轨迹的经纬度坐标bb
			//经纬度坐标是以","进行分割的
			String[] longitudeDismension = bb.split(",");
			//Double.valueOf()把string类型转换为double类型
			longitude1 = Double.valueOf(longitudeDismension[0]);
			dimension1 = Double.valueOf(longitudeDismension[1]);
			
			longitude2 = Double.valueOf(longitudeDismension[2]);
			dimension2 = Double.valueOf(longitudeDismension[3]);
			
			//计算具有经纬度的两坐标间的距离
			double bbDistance = distanceAndTime.
					getDistance(longitude1, dimension1, longitude2, dimension2);
			
			//解析pts中的内容
			JSONArray ptsjsonArray = (JSONArray)jsonObject.get("pts");
			Iterator iterator = ptsjsonArray.iterator();
			
			//解析pts中标签6的内容,并得到第一个标签6的内容
			while(iterator.hasNext()){
				JSONObject pts6JSONObject = (JSONObject)iterator.next();
				pts6Time = pts6JSONObject.get("6").toString();
				pts6FirstTime = pts6Time.split("\r\n");
				break;//对于标签6的时间,只要第一个时间
			}
			
			//解析pts中标签7的内容,并得到最后一个标签7的内容
			while(iterator.hasNext()){
				JSONObject pts7jsonobject = (JSONObject)iterator.next();
				ptsTime7 = pts7jsonobject.get("7").toString();
			}
			
			//计算轨迹的时间差,单位为小时(H)
			Date date1 = dateFormat.parse(ptsTime7);
			Date date2 = dateFormat.parse(pts6FirstTime[0]);
			betweenTime = (date1.getTime()-date2.getTime())/1000.0/3600.0;
			
			reslut = _id+"\t"+bbDistance+"\t"+betweenTime;
			writeToLocal(reslut);
//			System.out.println(reslut);
		}
		
	}
	
	//已知经纬度的轨迹点,计算两个轨迹点之间的距离,单位为KM
	public double getDistance(double longitude1,double dimension1,double longitude2,double dimension2){
		double x,y,distance;
		x = (longitude1 - longitude2)*PI*earthRadius*
				Math.cos(((dimension1+dimension2)/2)*PI/180)/180;
		y = (dimension1-dimension2)*PI*earthRadius/180;
		distance = Math.hypot(x, y);
		return distance;
	}
	
	//计算结果输出
	public static void writeToLocal(String string) throws IOException{
		File outFile = new File("D://0820--Study//轨迹点数据//result.json");
		if (!outFile.exists()) {
			outFile.createNewFile();
		}
		
		//FileOutputStream设置为true,是为了后一条结果记录覆盖前一条结果记录
		FileOutputStream fileOutputStream = new FileOutputStream(outFile,true);
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
		BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
		bufferedWriter.write(string);
		bufferedWriter.newLine();
		bufferedWriter.flush();
		
		bufferedWriter.close();
		outputStreamWriter.close();
		fileOutputStream.close();
	}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值