package com.zy.common.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import java.io.*; import java.util.ArrayList; /** * A*算法地图获取类 */ public class NavigateMapData { public int[][] getData() { return getData("in"); } public int[][] getData(String mapType) { try { String mapFilename = ""; if (mapType.equals("in")) { mapFilename = "mapIn.json"; }else { mapFilename = "mapOut.json"; } String fileName = this.getClass().getClassLoader().getResource(mapFilename).getPath();//获取文件路径 File file = new File(fileName); StringBuffer stringBuffer = new StringBuffer(); if (file.isFile() && file.exists()) { InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK"); BufferedReader br = new BufferedReader(isr); String lineTxt = null; while ((lineTxt = br.readLine()) != null) { stringBuffer.append(lineTxt); } br.close(); //解析json地图数据 ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class); int[][] map = new int[arrayList.size()][]; int j = 0; for (Object obj : arrayList) { ArrayList list = JSON.parseObject(obj.toString(), ArrayList.class); int[] tmp = new int[list.size()]; int i = 0; for (Object o : list) { JSONObject jsonObject = JSON.parseObject(o.toString()); //将数据添加进二维数组 tmp[i++] = Integer.parseInt(jsonObject.get("value").toString()); } //数据添加进一维数组 map[j++] = tmp; } return map; } else { System.out.println("文件不存在!"); } } catch (IOException ioException) { ioException.printStackTrace(); } return null; } }