java如何實現(xiàn)抽取json文件指定字段值
使用場景
我有一個5000條數(shù)據(jù)的json文件,每條數(shù)據(jù)包含地名、該地的經(jīng)緯度等其他很多信息?,F(xiàn)在想把地名和經(jīng)緯度抽出來導入到數(shù)據(jù)庫中。
navicat自帶的導入json格式文件不好用,只能導入json文件中的外層數(shù)據(jù),而我需要的地名和經(jīng)緯度信息在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": "某某社區(qū)", "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>
代碼實現(xiàn)
讀取本地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數(shù)組 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數(shù)組 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; } } }
把需要的數(shù)據(jù)整理到excel中
如上例所示,我需要地名和經(jīng)緯度,可以一次性查出三列數(shù)據(jù),但是為了復制到excel比較方便,我選擇一個一個查出來,逐個復制到excel中,再為每列起個列名,就能非常方便地使用navicat的”導入xls“功能導入大量數(shù)據(jù)啦~
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java生成隨機數(shù)之Random與ThreadLocalRandom性能比較詳解
大家項目中如果有生成隨機數(shù)的需求,我想大多都會選擇使用Random來實現(xiàn),它內部使用了CAS來實現(xiàn)。?實際上,JDK1.7之后,提供了另外一個生成隨機數(shù)的類ThreadLocalRandom,那么他們二者之間的性能是怎么樣的呢?本文就來詳細說說2022-12-12