用Jackson取得城市json的经纬度

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import util.JtFtConvert;

public class CityPoint {
	
	public static void main(String[] args) {
            // 这里参数是文件夹的路径
             CityPoint.GetFile("json");
	}

	//读取文件目录
	public static void GetFile(String path) { 
			File file = new File(path);
			File[] files = file.listFiles();
			if(files != null &&files.length >0){
				for(File f : files){
					// 判断是不是json文件,这文件只存放json,就循环一遍
					if(f.getName().substring(f.getName().indexOf(".")+1).equals("json")){
						String QPath = path + "/" + f.getName();
						CityPoint.GetDataByJackson(QPath);
					}
					
				}
			}
	}
	
	// 用Jackson获取数据
	public static void GetDataByJackson(String path) { 
    	ObjectMapper objectMapper = null;
        BufferedReader br = null;
        
	    try {
	    	
	    	// 初始配置, maps的类型是根据要取得json格式变化的,{}包含的用Map集合,[]包含的用List集合
	    	objectMapper = new ObjectMapper();
	    	br = new BufferedReader(new FileReader(path));
	    	Map<String, List<Map<String, Map<String, List<Map<String, Object>>>>>> maps =  
					objectMapper.readValue(br, Map.class);
	        DecimalFormat df = new DecimalFormat("0.00");
	        
	    	// 省份标题
	    	path = path.substring(path.lastIndexOf("/")+1, path.indexOf("."));
	    	System.out.println();
			System.out.println("//------------------------" + path + "-------------------------------------");
	        
	        // 循环打印
	        for(int i=0; i<maps.get("features").size(); i++){
	        	
	        	// 每条数据后加逗号,最后一条不加判断
	        	if(i != 0) System.out.println(",");
	        	
	        	// 取数据
	        	Map<String, List<Map<String, Object>>> mapOfProperties = 
	        			maps.get("features").get(i).get("properties");
	        	String lng = ((Object)mapOfProperties.get("cp").get(0)).toString();
	        	String lat = ((Object)mapOfProperties.get("cp").get(1)).toString();
	        	String cityName = ((Object)mapOfProperties.get("name")).toString();
	        	
	        	// 格式化
	        	lng = df.format(Double.parseDouble(lng));
	        	lat = df.format(Double.parseDouble(lat));
	        	if(path.equals("aomen") || path.equals("xianggang") || path.equals("taiwan"))
	        		cityName = new JtFtConvert().convert(cityName, 0); // 繁体转简体
	        	
	        	// 打印
	        	System.out.print("\"" + cityName + "\":[" + lng + "," + lat + "]");
	        }
	    } catch (JsonParseException e) {
	        e.printStackTrace();
	    } catch (JsonMappingException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    } finally{
	    	try{
	    		if(br != null){
	    			br.close();
	    		}
	    	} catch(IOException e){
	    		e.printStackTrace();
	    	}
	    }
	}
	
	// 用字符串截取获得数据,很乱,没什么用。
	public static void GetDataByCutout(String path) { 
		ObjectMapper objectMapper = null;
	    BufferedReader br = null;
	    try {
	    	objectMapper = new ObjectMapper();
	    	br = new BufferedReader(new FileReader(path));
	    	path = path.substring(5, path.indexOf("."));
			System.out.println("//------------------------" + path + "-------------------------------------");
	        Map<String, Object> maps = objectMapper.readValue(br, Map.class);
	        
	        String str = maps.get("features").toString();
	        DecimalFormat df = new DecimalFormat("0.00");
	        while(str.indexOf("cp=") != -1){
	        	
	        	int index = str.indexOf("cp=");
	        	str = str.substring(index+4);
	        	String start = str.substring(0,str.indexOf(","));
	        	start = df.format(Double.parseDouble(start));
	        	str = str.substring(str.indexOf(",")+1);
	        	String end = str.substring(0, str.indexOf("]"));
	        	end = df.format(Double.parseDouble(end));
	        	str = str.substring(str.indexOf("name")+5);
	        	String name = str.substring(0,str.indexOf(","));
	        	if(path.equals("aomen") || path.equals("xianggang") || path.equals("taiwan"))
	        		name = new JtFtConvert().convert(name, 0);
	        	
	        	System.out.print("\"" + name + "\":[" + start + "," + end + "]");
		        if(str.indexOf("cp=") != -1) System.out.println(",");
		        else System.out.println();
	        }
	        
	        
	    } catch (JsonParseException e) {

	        e.printStackTrace();

	    } catch (JsonMappingException e) {
	        e.printStackTrace();

	    } catch (IOException e) {

	        e.printStackTrace();

	    }

	}
}

研究Jackson时主要是看hoojo的文章,链接:http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html。

jackson的jar包是百度来的,很方便。

json文件从Echarts上载来的。http://echarts.baidu.com/download-map.html。

具体文件位置看图片


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值