java如何實(shí)現(xiàn)抽取json文件指定字段值
使用場(chǎng)景
我有一個(gè)5000條數(shù)據(jù)的json文件,每條數(shù)據(jù)包含地名、該地的經(jīng)緯度等其他很多信息?,F(xiàn)在想把地名和經(jīng)緯度抽出來(lái)導(dǎo)入到數(shù)據(jù)庫(kù)中。
navicat自帶的導(dǎo)入json格式文件不好用,只能導(dǎo)入json文件中的外層數(shù)據(jù),而我需要的地名和經(jīng)緯度信息在json的內(nèi)層。
抽取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": ""
}
}
]
}導(dǎo)入依賴(lài)
在pom.xml文件里加上fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.9</version>
</dependency>代碼實(shí)現(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下面(直接復(fù)制到resourses)
String path = ddem.class.getClassLoader().getResource("villagePoint.json").getPath();
String s = readJsonFile(path);
JSONObject jobj = JSON.parseObject(s);
JSONArray features = jobj.getJSONArray("features");//構(gòu)建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很多種類(lèi)型,具體用哪個(gè)看你自己的json內(nèi)容


完整代碼
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");//構(gòu)建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ù),但是為了復(fù)制到excel比較方便,我選擇一個(gè)一個(gè)查出來(lái),逐個(gè)復(fù)制到excel中,再為每列起個(gè)列名,就能非常方便地使用navicat的”導(dǎo)入xls“功能導(dǎo)入大量數(shù)據(jù)啦~
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java類(lèi)的訪問(wèn)權(quán)限關(guān)鍵字用法說(shuō)明
這篇文章主要介紹了Java類(lèi)的訪問(wèn)權(quán)限關(guān)鍵字用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
Map集合中獲取key-value值的實(shí)現(xiàn)方法
這篇文章主要介紹了Map集合中獲取key-value值的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Junit 5中@ParameterizedTest與@EnumSource結(jié)合使用
今天小編就為大家分享一篇關(guān)于Junit 5中@ParameterizedTest與@EnumSource結(jié)合使用,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
MyBatis在Mapper中傳遞多個(gè)參數(shù)的四種方法詳解
這篇文章主要介紹了MyBatis在Mapper中傳遞多個(gè)參數(shù)的四種方法,文章通過(guò)代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,,需要的朋友可以參考下2023-09-09
SpringSecurity拋出異常但AccessDeniedHandler不生效的解決
本文主要介紹了SpringSecurity拋出異常但AccessDeniedHandler不生效的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
mybatis入門(mén)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了mybatis入門(mén)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
詳解MyBatis-Puls中saveBatch批量添加慢的問(wèn)題
本文主要介紹了詳解MyBatis-Puls中saveBatch批量添加慢的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

