最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java解析json數(shù)組方式

 更新時(shí)間:2023年06月02日 10:23:09   作者:阿懵  
這篇文章主要介紹了java解析json數(shù)組方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

java解析json數(shù)組

最簡(jiǎn)單的json數(shù)組

[
? ? {
? ? ? ? "sisid": 2015111234,
? ? ? ? "sitid": 20001
? ? },
? ? {
? ? ? ? "sisid": 2015112312,
? ? ? ? "sitid": 20003
? ? }
]

其對(duì)應(yīng)的內(nèi)容為:

為什么我說這是最簡(jiǎn)單的json數(shù)組呢?因?yàn)檫@個(gè)json數(shù)組連json對(duì)象名都省略了。

如果加上對(duì)象名是這樣的:

{
? ? "msg": [
? ? ? ? {
? ? ? ? ? ? "sisid": 2015111234,
? ? ? ? ? ? "sitid": 20001
? ? ? ? },
? ? ? ? {
? ? ? ? ? ? "sisid": 2015112312,
? ? ? ? ? ? "sitid": 20003
? ? ? ? }
? ? ]
}

我看json數(shù)組結(jié)構(gòu)是這樣看的:最外面一層是一個(gè)方括號(hào)表示這是一個(gè)json數(shù)組,內(nèi)部是連個(gè)花括號(hào)表示包含兩個(gè)json對(duì)象(且注意到花括號(hào)外面沒有對(duì)象名),且這兩個(gè)對(duì)象分別是這個(gè)json數(shù)組的第0項(xiàng)和第1項(xiàng)。

如何解析這個(gè)json數(shù)組

        //解析json數(shù)組
		for (int i = 0; i < json.size(); i++) {
			JsonObject signin = (JsonObject) json.get(i);
			JsonElement int_sisid = signin.get("sisid");
			JsonElement int_sitid = signin.get("sitid");
            //獲取sisid
			String SISID = String.valueOf(int_sisid);
            //獲取sitid
			String SITID = String.valueOf(int_sitid);
		}

注:我使用的是Gson的jar包

for循環(huán)獲取數(shù)組中個(gè)每個(gè)對(duì)象元素JsonObject,再通過get(“屬性名”)獲取這個(gè)對(duì)象中的所對(duì)應(yīng)的元素JsonElement,最后轉(zhuǎn)化為String類型。

java中JSON數(shù)據(jù)的讀取和解析

在做springboot項(xiàng)目時(shí)用到了json文件讀取和解析,所以在這里記錄一下學(xué)習(xí)過程中總結(jié)的一些點(diǎn),希望對(duì)大家有幫助~

配置fastJson

<!--引入fastjson依賴-->
<dependency>
? ? <groupId>com.alibaba</groupId>
? ? <artifactId>fastjson</artifactId>
? ? <version>1.2.35</version>
</dependency>

構(gòu)建工具類(方便多次調(diào)用時(shí)重復(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對(duì)象中的"features"字段內(nèi)容,是數(shù)組類型的,采用以下方式:

// 方式一
JSONArray featureArray = JSON.parseArray(jsonObejct.get("features").toString());
// 方式二
JSONArray featureArray = jsonObejct.getJSONArray("features");

讀取對(duì)象類型字段

// 方式一
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ù)類型

// 方式一
String type = jsonObejct.get("type").toString();
// 方式二
String type = jsonObejct.getString("type");

整體解析

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ù)組第一個(gè)元素,為地區(qū)對(duì)象
JSONObject regionObject = JSONObject.parseObject(featureArray.get(0).toString());
// 讀取地區(qū)對(duì)象中的參數(shù)對(duì)象
JSONObject propertiesObject = JSONObject.parseObject(regionObject.getString("properties"));
// 讀取參數(shù)對(duì)象的名稱
String name = propertiesObject.getString("name");
// 讀取參數(shù)對(duì)象的地區(qū)代碼
int adcode = propertiesObject.getIntValue("adcode");
// 讀取地區(qū)對(duì)象的幾何對(duì)象
JSONObject geometryObject = JSONObject.parseObject(regionObject.get("geometry").toString());
// 讀取幾何字段中的坐標(biāo)數(shù)組
JSONArray coordinates = JSONObject.parseArray(geometryObject.get("coordinates").toString());
// 讀取幾何對(duì)象中的類型名稱
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");

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • mybatis-plus?Wrapper條件構(gòu)造器updateForSet更新方式

    mybatis-plus?Wrapper條件構(gòu)造器updateForSet更新方式

    這篇文章主要介紹了mybatis-plus?Wrapper條件構(gòu)造器updateForSet更新方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • java內(nèi)部類的定義與分類示例詳解

    java內(nèi)部類的定義與分類示例詳解

    這篇文章主要給大家介紹了關(guān)于java內(nèi)部類的定義與分類的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Java數(shù)據(jù)結(jié)構(gòu)之優(yōu)先級(jí)隊(duì)列(PriorityQueue)用法詳解

    Java數(shù)據(jù)結(jié)構(gòu)之優(yōu)先級(jí)隊(duì)列(PriorityQueue)用法詳解

    優(yōu)先級(jí)隊(duì)列是一種先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu),操作的數(shù)據(jù)帶有優(yōu)先級(jí),這種數(shù)據(jù)結(jié)構(gòu)就是優(yōu)先級(jí)隊(duì)列(PriorityQueue)。本文將詳細(xì)講講Java優(yōu)先級(jí)隊(duì)列的用法,感興趣的可以了解一下
    2022-07-07
  • Java thrift服務(wù)器和客戶端創(chuàng)建實(shí)例代碼

    Java thrift服務(wù)器和客戶端創(chuàng)建實(shí)例代碼

    Thrift是一個(gè)軟件框架,用來進(jìn)行可擴(kuò)展且跨語言的服務(wù)的開發(fā)。接下來通過本文給大家介紹Java thrift服務(wù)器和客戶端創(chuàng)建實(shí)例代碼,需要的朋友參考下吧
    2017-04-04
  • 最新評(píng)論

    巨野县| 洛隆县| 开鲁县| 永年县| 东阳市| 中山市| 吉首市| 高要市| 牡丹江市| 柞水县| 永康市| 内丘县| 长沙市| 宾川县| 云林县| 乌拉特后旗| 东海县| 六盘水市| 中西区| 安平县| 当涂县| 綦江县| 梓潼县| 石城县| 栾川县| 宣城市| 吴川市| 荣昌县| 定州市| 文登市| 建湖县| 措美县| 会同县| 余干县| 轮台县| 石柱| 中卫市| 镇康县| 武清区| 金坛市| 通道|