java解析json数组方式
作者:阿懵 发布时间:2023-08-10 17:22:14
标签:java,json,数组
java解析json数组
最简单的json数组
[
{
"sisid": 2015111234,
"sitid": 20001
},
{
"sisid": 2015112312,
"sitid": 20003
}
]
其对应的内容为:
为什么我说这是最简单的json数组呢?因为这个json数组连json对象名都省略了。
如果加上对象名是这样的:
{
"msg": [
{
"sisid": 2015111234,
"sitid": 20001
},
{
"sisid": 2015112312,
"sitid": 20003
}
]
}
我看json数组结构是这样看的:最外面一层是一个方括号表示这是一个json数组,内部是连个花括号表示包含两个json对象(且注意到花括号外面没有对象名),且这两个对象分别是这个json数组的第0项和第1项。
如何解析这个json数组
//解析json数组
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循环获取数组中个每个对象元素JsonObject,再通过get(“属性名”)获取这个对象中的所对应的元素JsonElement,最后转化为String类型。
java中JSON数据的读取和解析
在做springboot项目时用到了json文件读取和解析,所以在这里记录一下学习过程中总结的一些点,希望对大家有帮助~
配置fastJson
构建工具类(方便多次调用时重复使用)
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","该地区GeoJson文件不存在!");
return notFoundJson;
} catch (IOException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonString);
}
json文件示例(以geojson为例,数据结构比较复杂,只是层次比较多)
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"adcode": 110101,
"name": "东城区",
"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
]
]
]
]
}
}
]
}
调用工具类读取数据
String filePath = "文件路径";
// 读取json文件
JSONObject jsonObejct = readJsonFile(filePath);
读取json对象中的"features"字段内容,是数组类型的,采用以下方式:
// 方式一
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");
读取整数类型
// 方式一
String type = jsonObejct.get("type").toString();
// 方式二
String type = jsonObejct.getString("type");
整体解析
String filePath = "文件地址/文件名.json";
JSONObject jsonObejct = ReadJsonUtils.readJsonFile(filePath);
// 方式一(很复杂,语句分开,但是结构清晰)
// 读取json文件的features字段,并转换为json数组
JSONArray featureArray = JSON.parseArray(jsonObejct.get("features").toString());
// 读取数组第一个元素,为地区对象
JSONObject regionObject = JSONObject.parseObject(featureArray.get(0).toString());
// 读取地区对象中的参数对象
JSONObject propertiesObject = JSONObject.parseObject(regionObject.getString("properties"));
// 读取参数对象的名称
String name = propertiesObject.getString("name");
// 读取参数对象的地区代码
int adcode = propertiesObject.getIntValue("adcode");
// 读取地区对象的几何对象
JSONObject geometryObject = JSONObject.parseObject(regionObject.get("geometry").toString());
// 读取几何字段中的坐标数组
JSONArray coordinates = JSONObject.parseArray(geometryObject.get("coordinates").toString());
// 读取几何对象中的类型名称
String type = geometryObject.getString("type");
// 方式二(无需每次重新转换类型,一行搞定)
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");
来源:https://blog.csdn.net/mm1030533738/article/details/81154485
0
投稿
猜你喜欢
- 什么是接口:接口是一系列方法的声明,是一些方法特征的集合注意:在接口中只有方法名,没有方法体!关键字:interface(创建接口), im
- 在了解HTTP断点续传的原理之前,让我们先来了解一下HTTP协议,HTTP协议是一种基于tcp的简单协议,分为请求和回复两种。请求协议是由客
- 本文实例讲述了C#执行外部命令的方法。分享给大家供大家参考。具体实现方法如下:///<summary>///executes a
- select 相当于 for 循环select id from IDArrayLinkedList a = new LinkedList()
- Controller简介Controller控制器,是MVC中的部分C,为什么是部分呢?因为此处的控制器主要负责功能处理部分:1、收集、验证
- 斗地主小游戏之洗牌发牌任务描述编写一个斗地主发牌洗牌的程序,要求按照斗地主的规则完成洗牌发牌的过程,牌面由花色色和数字(包括J,Q,K,A字
- 1. 下载Tomcat首先,下载Apache Tomcat并解压到本地计算机,可存放于任何位置。另外,需要在系统中环境JRE_H
- 本文实例为大家分享了unity shader实现光照效果的具体代码,供大家参考,具体内容如下效果图:shader被附给了球。灯光需要在属性面
- 1. 三种常用的字符串判空串方法:Length法:bool isEmpty = (str.Length == 0);Empty法:bool
- 前言:Timer是一个定时器,作为C#开发Timer控件是我们用的比较多的一个控件,它的功能很简单,但是也是值得我们去学习的一个知识点,今天
- 要想实现android手机通过扫描名片,得到名片信息,可以使用脉可寻提供的第三方SDK,即Maketion ScanCard SDK,脉可寻
- Java常用API介绍API概念什么是API?API(Application Programming interface) 应用程序编程接口
- 碎片,它的出现是为了更好展示UI的设计,让程序更加得到充分的展示。Fragment的出现,如微信的额主界面包含多个Fragment,使得微信
- 上来就给点干货吧利用脚本,一键设置java环境变量(默认安装路径)@echo offcolor 0aecho.---------------
- 意图:想将项目用到的两个dll库文件(CryptEnDe.dll和ICSharpCode.SharpZipLib.dll)一同编译进exe中
- 本文实例讲述了C#画笔使用复合数组绘制单个矩形的方法。分享给大家供大家参考。具体实现方法如下:using System;using Syst
- 在一个项目中,如果我们既用到了Struts2又用到了Servlet,项目运行时有可能无法正常访问Servlet,原因是在配置Struts的过
- 前言本来没有计划这一篇文章的,只是在看完SpringBoot核心原理后,突然想到之前开发中遇到的MVC自动失效的问题,虽然网上有很多文章以及
- 简介关于IDEA的介绍,引用自百度百科:IDEA全称 IntelliJ IDEA,是java编程语言开发的集成环境。IntelliJ在业界被
- MyBatisMyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC