java抽取json文件指定字段值
偷偷写博客567 人气:2使用场景
我有一个5000条数据的json文件,每条数据包含地名、该地的经纬度等其他很多信息。现在想把地名和经纬度抽出来导入到数据库中。
navicat自带的导入json格式文件不好用,只能导入json文件中的外层数据,而我需要的地名和经纬度信息在json的内层。
抽取json指定字段值
json文件格式
{ "type": "FeatureCollection", "features": [{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [117.135437, 39.22393] }, "properties": { "type": 1, "OBJECTID": 9288, "CC": "1117", "GB": "310107", "NAME": "某某村", "PAC": "120104008006111", "ELEMSTIME": "20150630", "ELEMETIME": "", "AREACODE": 120000, "FEATID": 120000403, "ChangeType": 0, "ChangeAtt": "" } }, { "type": "Feature", "geometry": { "type": "Point", "coordinates": [117.17052343, 39.124663697000074] }, "properties": { "OBJECTID": 9289, "CC": "1117", "GB": "310107", "NAME": "某某社区", "PAC": "120104003014111", "ELEMSTIME": "20150630", "ELEMETIME": "", "AREACODE": 120000, "FEATID": 120000458, "ChangeType": 0, "ChangeAtt": "" } } ] }
导入依赖
在pom.xml文件里加上fastjson
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.9</version> </dependency>
代码实现
读取本地json文件的方法
public static String readJsonFile(String fileName) { String jsonStr = ""; try { File jsonFile = new File(fileName); FileReader fileReader = new FileReader(jsonFile); Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8"); int ch = 0; StringBuffer sb = new StringBuffer(); while ((ch = reader.read()) != -1) { sb.append((char) ch); } fileReader.close(); reader.close(); jsonStr = sb.toString(); return jsonStr; } catch (IOException e) { e.printStackTrace(); return null; } }
读取指定字段
先把json文件放在resourses下面(直接复制到resourses)
String path = ddem.class.getClassLoader().getResource("villagePoint.json").getPath(); String s = readJsonFile(path); JSONObject jobj = JSON.parseObject(s); JSONArray features = jobj.getJSONArray("features");//构建JSONArray数组 for (int i = 0; i < features.size(); i++) { JSONObject key = (JSONObject) features.get(i); JSONObject geometry =key.getJSONObject("geometry"); JSONArray coordinates=geometry.getJSONArray("coordinates"); BigDecimal jingdu = coordinates.getBigDecimal(0); BigDecimal weidu = coordinates.getBigDecimal(1); JSONObject properties=key.getJSONObject("properties"); String name =(String)properties.getString("NAME"); // System.out.println(jingdu); // System.out.println(weidu); System.out.println(name); }
JSONObject或JSONArray可以get很多种类型,具体用哪个看你自己的json内容
完整代码
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.io.*; import java.math.BigDecimal; public class ddem { public static void main(String[] args) { String path = ddem.class.getClassLoader().getResource("villagePoint.json").getPath(); String s = readJsonFile(path); JSONObject jobj = JSON.parseObject(s); JSONArray features = jobj.getJSONArray("features");//构建JSONArray数组 for (int i = 0; i < features.size(); i++) { JSONObject key = (JSONObject) features.get(i); JSONObject geometry =key.getJSONObject("geometry"); JSONArray coordinates=geometry.getJSONArray("coordinates"); BigDecimal jingdu = coordinates.getBigDecimal(0); BigDecimal weidu = coordinates.getBigDecimal(1); JSONObject properties=key.getJSONObject("properties"); String name =(String)properties.getString("NAME"); // System.out.println(jingdu); // System.out.println(weidu); System.out.println(name); } } //读取json文件 public static String readJsonFile(String fileName) { String jsonStr = ""; try { File jsonFile = new File(fileName); FileReader fileReader = new FileReader(jsonFile); Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8"); int ch = 0; StringBuffer sb = new StringBuffer(); while ((ch = reader.read()) != -1) { sb.append((char) ch); } fileReader.close(); reader.close(); jsonStr = sb.toString(); return jsonStr; } catch (IOException e) { e.printStackTrace(); return null; } } }
把需要的数据整理到excel中
如上例所示,我需要地名和经纬度,可以一次性查出三列数据,但是为了复制到excel比较方便,我选择一个一个查出来,逐个复制到excel中,再为每列起个列名,就能非常方便地使用navicat的”导入xls“功能导入大量数据啦~
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
加载全部内容