? ? ? ? 在做springboot項目時用到了json文件讀取和解析,所以在這里記錄一下學(xué)習(xí)過程中總結(jié)的一些點,希望對大家有幫助~
-
配置fastJson
<!--引入fastjson依賴-->
<dependency>
? ?<groupId>com.alibaba</groupId>
? ?<artifactId>fastjson</artifactId>
? ?<version>1.2.35</version>
</dependency>
-
構(gòu)建工具類(方便多次調(diào)用時重復(fù)使用)
public static JSONObject readJsonFile(String filename){
? ? ? ?String jsonString = "";
? ? ? ?File jsonFile = new File(filename);
? ? ? ?try {
? ? ? ? ? ?FileReader fileReader = new FileReader(jsonFile);
? ? ? ? ? ?Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
? ? ? ? ? ?int ch = 0;
? ? ? ? ? ?StringBuffer stringBuffer = new StringBuffer();
? ? ? ? ? ?while ((ch = reader.read()) != -1){
? ? ? ? ? ? ? ?stringBuffer.append((char) ch);
? ? ? ? ? }
? ? ? ? ? ?fileReader.close();
? ? ? ? ? ?reader.close();
? ? ? ? ? ?jsonString = stringBuffer.toString();
? ? ? } catch (FileNotFoundException e){
? ? ? ? ? ?JSONObject notFoundJson = new JSONObject();
? ? ? ? ? ?notFoundJson.put("code",Code.GET_ERR);
? ? ? ? ? ?notFoundJson.put("msg","該地區(qū)GeoJson文件不存在!");
? ? ? ? ? ?return notFoundJson;
? ? ? } catch (IOException e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? }
? ? ? ?return JSONObject.parseObject(jsonString);
? }
-
json文件示例(以geojson為例,數(shù)據(jù)結(jié)構(gòu)比較復(fù)雜,只是層次比較多)
{
? ?"type": "FeatureCollection",
? ?"features": [
? ? ? {
? ? ? ? ? ?"type": "Feature",
? ? ? ? ? ?"properties": {
? ? ? ? ? ? ? ?"adcode": 110101,
? ? ? ? ? ? ? ?"name": "東城區(qū)",
? ? ? ? ? ? ? ?"center": [
? ? ? ? ? ? ? ? ? ?116.418757,
? ? ? ? ? ? ? ? ? ?39.917544
? ? ? ? ? ? ? ],
? ? ? ? ? ? ? ?"centroid": [
? ? ? ? ? ? ? ? ? ?116.416739,
? ? ? ? ? ? ? ? ? ?39.912912
? ? ? ? ? ? ? ],
? ? ? ? ? ? ? ?"childrenNum": 0,
? ? ? ? ? ? ? ?"level": "district",
? ? ? ? ? ? ? ?"acroutes": [
? ? ? ? ? ? ? ? ? ?100000,
? ? ? ? ? ? ? ? ? ?110000
? ? ? ? ? ? ? ],
? ? ? ? ? ? ? ?"parent": {
? ? ? ? ? ? ? ? ? ?"adcode": 110000
? ? ? ? ? ? ? }
? ? ? ? ? },
? ? ? ? ? ?"geometry": {
? ? ? ? ? ? ? ?"type": "MultiPolygon",
? ? ? ? ? ? ? ?"coordinates": [
? ? ? ? ? ? ? ? ? [
? ? ? ? ? ? ? ? ? ? ? [
? ? ? ? ? ? ? ? ? ? ? ? ? [
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?116.387664,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?39.960923
? ? ? ? ? ? ? ? ? ? ? ? ? ],
? ? ? ? ? ? ? ? ? ? ? ? ? [
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?116.38948,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?39.961038
? ? ? ? ? ? ? ? ? ? ? ? ? ],
? ? ? ? ? ? ? ? ? ? ? ? ? [
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?116.389506,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?39.963147
? ? ? ? ? ? ? ? ? ? ? ? ? ],
? ? ? ? ? ? ? ? ? ? ? ? ? [
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?116.396959,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?39.963204
? ? ? ? ? ? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? ]
? ? ? ? ? }
? ? ? }
? ]
}
-
調(diào)用工具類讀取數(shù)據(jù):
String filePath = "文件路徑";
// 讀取json文件
JSONObject jsonObejct = readJsonFile(filePath);
-
讀取json對象中的"features"字段內(nèi)容,是數(shù)組類型的,采用以下方式:
// 方式一
JSONArray featureArray = JSON.parseArray(jsonObejct.get("features").toString());
// 方式二
JSONArray featureArray = jsonObejct.getJSONArray("features");
-
讀取對象類型字段:
// 方式一
JSONObject propertiesObject = JSONObject.parseObject(regionObject.getString("properties"));
// 方式二
JSONObject propertiesObject = jsonObejct.getJSONArray("features").getJSONObject(0).getJSONObject("properties");
-
讀取字符串類型:
// 方式一
String type = jsonObejct.get("type").toString();
// 方式二
String type = jsonObejct.getString("type");
-
讀取整數(shù)類型:文章來源:http://www.zghlxwxcb.cn/news/detail-520877.html
// 方式一
String type = jsonObejct.get("type").toString();
// 方式二
String type = jsonObejct.getString("type");
-
整體解析:文章來源地址http://www.zghlxwxcb.cn/news/detail-520877.html
String filePath = "文件地址/文件名.json";
JSONObject jsonObejct = ReadJsonUtils.readJsonFile(filePath);
?
// 方式一(很復(fù)雜,語句分開,但是結(jié)構(gòu)清晰)
// 讀取json文件的features字段,并轉(zhuǎn)換為json數(shù)組
JSONArray featureArray = JSON.parseArray(jsonObejct.get("features").toString());
// 讀取數(shù)組第一個元素,為地區(qū)對象
JSONObject regionObject = JSONObject.parseObject(featureArray.get(0).toString());
// 讀取地區(qū)對象中的參數(shù)對象
JSONObject propertiesObject = JSONObject.parseObject(regionObject.getString("properties"));
// 讀取參數(shù)對象的名稱
String name = propertiesObject.getString("name");
// 讀取參數(shù)對象的地區(qū)代碼
int adcode = propertiesObject.getIntValue("adcode");
// 讀取地區(qū)對象的幾何對象
JSONObject geometryObject = JSONObject.parseObject(regionObject.get("geometry").toString());
// 讀取幾何字段中的坐標(biāo)數(shù)組
JSONArray coordinates = JSONObject.parseArray(geometryObject.get("coordinates").toString());
// 讀取幾何對象中的類型名稱
String type = geometryObject.getString("type");
?
// 方式二(無需每次重新轉(zhuǎn)換類型,一行搞定)
String name = jsonObejct.getJSONArray("features").getJSONObject(0).getJSONObject("properties").getString("name");
String type = jsonObejct.getJSONArray("features").getJSONObject(0).getJSONObject("geometry").getString("type");
JSONArray coordinates = jsonObejct.getJSONArray("features").getJSONObject(0).getJSONObject("geometry").getJSONArray("coordinates");
到了這里,關(guān)于Java中JSON數(shù)據(jù)的讀取和解析的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!