java多层嵌套集合的json,如何获取多层集合中所需要的value
就很简单的一个问题折磨了整整一天,好在压抑的心情终于释放了,终于闲下来觉得是不是应该记录一下。
首先这个json串是从外网接口返回的数据,想要了解的朋友们可以复制到json解析器中看一下格式:https://www.json.cn/#
{"status":"True","biz_result":[{"PartyFunctionCode":"NBEPORT","PartyIdentifier":"container","PartyName":"宁波电子口岸","ManifestInfo":[{"LadingBillNumber":"COSU6224023460B","ShippingCompanyName":"","VesselNameCode":"COSCO YINGKOU","VesselName":"COSCO YINGKOU","PlaceOfLoading":"","PlaceOfDischarge":"","VoyageNumber":"130W","DirectionCode":"","VesselCallNumber":"","IMONumber":""}],"ContainerInfo":[{"ContainerNumber":"FCIU9840537","ContainerOperator":"COS","SealNumber":"11374417","CodeOfSizeAndType":"","ContainerLoadingStatusCode":"","ContainerTareWeight":"0","TransportMeansType":"","VehicleNumber":"","ContainerStatusInformation":[{"ImExIdentifier":"","TransportStatusCode":"码头已经放行","TransportStatusNameEN":"","DateOrTimeOrPeriod":"2019-10-25 17:34:49","TimeZoneIdentifier":"PRC","PlaceLocationQualifier":"NBEPORT","PlaceOrLocationIdentification":"","PlaceOrLocation":"","Remark":"宁波电子口岸","Status":"DOCKPASS","EventName":""},{"ImExIdentifier":"E","TransportStatusCode":"重箱进港","TransportStatusNameEN":"","DateOrTimeOrPeriod":"2019-10-24 13:29:00","TimeZoneIdentifier":"PRC","PlaceLocationQualifier":"NBEPORT","PlaceOrLocationIdentification":"BLCT3","PlaceOrLocation":"北仑四期码头","Remark":"宁波电子口岸","Status":"INOUT","EventName":"进港"},{"ImExIdentifier":"E","TransportStatusCode":"海关放行","TransportStatusNameEN":"","DateOrTimeOrPeriod":"2019-10-25 11:04:37","TimeZoneIdentifier":"PRC","PlaceLocationQualifier":"NBEPORT","PlaceOrLocationIdentification":"","PlaceOrLocation":"","Remark":"宁波电子口岸","Status":"CUSPASS","EventName":"海关放行"},{"ImExIdentifier":"","TransportStatusCode":"27301 装载舱单数据传输成功。","TransportStatusNameEN":"","DateOrTimeOrPeriod":"2019-10-31 13:44:41","TimeZoneIdentifier":"PRC","PlaceLocationQualifier":"NBEPORT","PlaceOrLocationIdentification":"","PlaceOrLocation":"","Remark":"宁波电子口岸","Status":"MFT","EventName":"装载舱单"},{"ImExIdentifier":"E","TransportStatusCode":"重箱装船","TransportStatusNameEN":"","DateOrTimeOrPeriod":"2019-11-01 16:13:00","TimeZoneIdentifier":"PRC","PlaceLocationQualifier":"NBEPORT","PlaceOrLocationIdentification":"BLCT3","PlaceOrLocation":"北仑四期码头","Remark":"宁波电子口岸","Status":"LOAD","EventName":"装船"},{"ImExIdentifier":"","TransportStatusCode":"开船","TransportStatusNameEN":"","DateOrTimeOrPeriod":"2019-11-01 22:00:00","TimeZoneIdentifier":"PRC","PlaceLocationQualifier":"NBEPORT","PlaceOrLocationIdentification":"BLCT3","PlaceOrLocation":"北仑四期码头","Remark":"宁波电子口岸","Status":"BERTH","EventName":"离港"}]}]},{"PartyFunctionCode":"NBEPORT","PartyIdentifier":"container","PartyName":"宁波电子口岸","ManifestInfo":[{"LadingBillNumber":"COSU6224023460A","ShippingCompanyName":"","VesselNameCode":"COSCO YINGKOU","VesselName":"COSCO YINGKOU","PlaceOfLoading":"","PlaceOfDischarge":"","VoyageNumber":"130W","DirectionCode":"","VesselCallNumber":"","IMONumber":""}],"ContainerInfo":[{"ContainerNumber":"FCIU9840537","ContainerOperator":"COS","SealNumber":"11374417","CodeOfSizeAndType":"","ContainerLoadingStatusCode":"","ContainerTareWeight":"0","TransportMeansType":"","VehicleNumber":"","ContainerStatusInformation":[{"ImExIdentifier":"","TransportStatusCode":"码头已经放行","TransportStatusNameEN":"","DateOrTimeOrPeriod":"2019-10-25 17:34:49","TimeZoneIdentifier":"PRC","PlaceLocationQualifier":"NBEPORT","PlaceOrLocationIdentification":"","PlaceOrLocation":"","Remark":"宁波电子口岸","Status":"DOCKPASS","EventName":""},{"ImExIdentifier":"E","TransportStatusCode":"重箱进港","TransportStatusNameEN":"","DateOrTimeOrPeriod":"2019-10-24 13:29:00","TimeZoneIdentifier":"PRC","PlaceLocationQualifier":"NBEPORT","PlaceOrLocationIdentification":"BLCT3","PlaceOrLocation":"北仑四期码头","Remark":"宁波电子口岸","Status":"INOUT","EventName":"进港"},{"ImExIdentifier":"E","TransportStatusCode":"海关放行","TransportStatusNameEN":"","DateOrTimeOrPeriod":"2019-10-25 11:04:27","TimeZoneIdentifier":"PRC","PlaceLocationQualifier":"NBEPORT","PlaceOrLocationIdentification":"","PlaceOrLocation":"","Remark":"宁波电子口岸","Status":"CUSPASS","EventName":"海关放行"},{"ImExIdentifier":"","TransportStatusCode":"27301 装载舱单数据传输成功。","TransportStatusNameEN":"","DateOrTimeOrPeriod":"2019-10-31 13:44:41","TimeZoneIdentifier":"PRC","PlaceLocationQualifier":"NBEPORT","PlaceOrLocationIdentification":"","PlaceOrLocation":"","Remark":"宁波电子口岸","Status":"MFT","EventName":"装载舱单"},{"ImExIdentifier":"E","TransportStatusCode":"重箱装船","TransportStatusNameEN":"","DateOrTimeOrPeriod":"2019-11-01 16:13:00","TimeZoneIdentifier":"PRC","PlaceLocationQualifier":"NBEPORT","PlaceOrLocationIdentification":"BLCT3","PlaceOrLocation":"北仑四期码头","Remark":"宁波电子口岸","Status":"LOAD","EventName":"装船"}]}]}],"error":""}
Json串
期间用了alibab的fastjson包想要一层一层剥开这个json,结果浪费了许多时间还是没有效果,估计是方法使用错了,这里就直接贴上成功的方法。
首先还是先建了实体类,这里比较简单,就是按照json的格式一层一层往下写,就直接贴代码了。(getset和构造器我就不贴了,有点太占空间了)
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.io.Serializable;
import java.util.List; @JsonIgnoreProperties(ignoreUnknown = true)
@SuppressWarnings("serial")
public class Result implements Serializable { private String status;
private List<biz_result> biz_result;
private String error;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.io.Serializable;
import java.util.List; @JsonIgnoreProperties(ignoreUnknown = true)
@SuppressWarnings("serial")
public class biz_result implements Serializable {
private String PartyFunctionCode;
private String PartyIdentifier;
private String PartyName;
private List<ManifestInfo> ManifestInfo ;
private List<ContainerInfo> ContainerInfo ;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.io.Serializable; @JsonIgnoreProperties(ignoreUnknown = true)
@SuppressWarnings("serial")
public class ManifestInfo implements Serializable {
private String LadingBillNumber;
private String ShippingCompanyName;
private String VesselNameCode;
private String VesselName;
private String PlaceOfLoading;
private String PlaceOfDischarge;
private String VoyageNumber;
private String DirectionCode;
private String VesselCallNumber;
private String IMONumber;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.io.Serializable;
import java.util.List; @JsonIgnoreProperties(ignoreUnknown = true)
@SuppressWarnings("serial")
public class ContainerInfo implements Serializable { private String ContainerNumber;
private String ContainerOperator;
private String SealNumber;
private String CodeOfSizeAndType;
private String ContainerLoadingStatusCode;
private String ContainerTareWeight;
private String TransportMeansType;
private String VehicleNumber;
private List<ContainerStatusInformation> ContainerStatusInformation ;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.io.Serializable; @JsonIgnoreProperties(ignoreUnknown = true)
@SuppressWarnings("serial")
public class ContainerStatusInformation implements Serializable { private String ImExIdentifier;
private String TransportStatusCode;
private String TransportStatusNameEN;
private String DateOrTimeOrPeriod;
private String TimeZoneIdentifier;
private String PlaceLocationQualifier;
private String PlaceOrLocationIdentification;
private String PlaceOrLocation;
private String Remark;
private String Status;
private String EventName;
才想起来要不要折叠一下,这会不会太长了,但是我实在太懒了,对不起我就不去重新操作了。
实体类建完以后我就尝试了一下,也不再使用alibab的fastjson,使用如下这个包:
import net.sf.json.JSONObject;
maven依赖如下:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
最终代码如下:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class Test02 { public static void main(String[]args){ String json = "这是上面巨长的JSON"; //得到json对象
JSONObject jsonObject = JSONObject.fromObject(json);
//获取其中的biz_result数组
JSONArray biz_result = jsonObject.getJSONArray("biz_result");
//获取数组中的某位元素
String biz_resultString = biz_result.getString(0);
//将数组中的这个数组再次转换成json对象
JSONObject jsonObject1 = JSONObject.fromObject(biz_resultString);
//通过getString获取value
String manifestInfo = jsonObject1.getString("ManifestInfo");
//去除首位中括号得到json格式
String substring = manifestInfo.substring(1);
String subString = substring.substring(0,substring.length()-1);
//得到json格式以后再转换成json对象
JSONObject jb3 = JSONObject.fromObject(subString);
//得到需要的值。
String vesselName = jb3.getString("VesselName");
System.out.println(vesselName); }
}
还是得养成写注释的习惯,要是不写注释贴上来,我自己都看着不舒服,最终得到了需要的数据:
COSCO YINGKOU
1234567.
java多层嵌套集合的json,如何获取多层集合中所需要的value的更多相关文章
- Java分享笔记:使用entrySet方法获取Map集合中的元素
/*--------------------------------- 使用entrySet方法取出Map集合中的元素: ....该方法是将Map集合中key与value的关系存入到了Set集合中,这 ...
- Java分享笔记:使用keySet方法获取Map集合中的元素
/*--------------------------- Map集合中利用keySet方法获取所有的元素值: ....keySet方法:将Map中的所有key值存入到Set集合中, ....利用Se ...
- Java如何接收前端传来的多层嵌套的复杂json串
想看问题直接解决方式,直接拉到博文底部. Spring的controller在接收前端传参的时候如果参数使用@RequestBody标注的时候 @RequestBody 则会把前端参数转为JSON的形 ...
- Java使用实现面向对象编程:第七章集合框架的解读=>重中之重
对于集合框架,是非常重要的知识,是程序员必须要知道的知识点. 但是我们为什么要引入集合框架呢? 我们之前用过数组存储数据,但是采用数组存储存在了很多的缺陷.而现在我们引用了集合框架,可以完全弥补了数组 ...
- DataTable/集合 转 Json
前端用的jqueryUI框架获取json格式数据绑定显示表格. 后端通过WebService获取的数据是DataTable. 现将获取DataTable转Json,也支持将数据集合转Json. 一.项 ...
- python字典嵌套字典的情况下获取某个key的value
最近在用python写接口的测试程序,期间用到解析字典获取某个key的value,由于多个接口返回的字典格式不是固定的并存在多层嵌套的情况.在字典的方法中也没有找到可直接达到目的的方法(也可能是我对字 ...
- Java 获取*.properties配置文件中的内容 ,常见的两种方法
import java.io.InputStream; import java.util.Enumeration; import java.util.List; import java.util.Pr ...
- 【SpringBoot】 Java中如何封装Http请求,以及JSON多层嵌套解析
前言 本文中的内容其实严格来说不算springboot里面的特性,属于JAVA基础,只是我在项目中遇到了,特归纳总结一下. HTTP请求封装 目前JAVA对于HTTP封装主要有三种方式: 1. JAV ...
- 【Java_Spring】java解析多层嵌套json字符串
java解析多层嵌套json字符串
- 多层嵌套的json数据
很多时候我们见到的json数据都是多层嵌套的,就像下面这般: {"name":"桔子桑", "sex":"男", , & ...
随机推荐
- int main (int argc, const char * argv[0]) 中参数的含义;指针数组和数组指针
恩,有的编译器初始化时候会产生这样的参数 argc是命令行总的参数个数,argv[]是argc个参数,其中第0个参数是程序的全名 1. 几种C++ 常见的参数种类 int main(void); in ...
- MySQL 库、表、记录、相关操作(1)
库.表.记录.相关操作(1) 数据库配置 # 通过配置文件统一配置的目的:统一管理 服务端(mysqld) .客户端(client) # 配置了 mysqld(服务端) 的编码为utf8,那么再创建的 ...
- c# 窗体开发4 数据库访问技术
ADO.NET的名称起源于ADO(ACTIVEX DATA OBJECTS) USING SYSTEM; USING SYSTEM.COLLECTIONS.GENERIC; USING SYSTEM. ...
- 切换控制器的三种手段push modal 切换window的rootViewController
- 利用tp5开发智慧软文发布系统中遇到的一些坑
1. PHP 计算两个时间戳之间相差的时间 假设你两个时间戳为$a,$b; 你可以用$c=$a-$b;(反正就是大的减小的),这时$c就是两个时间间隔的秒数了. 想求两个时间间隔的天数就用:$c/(6 ...
- phpStudy搭建PHP服务器
目录 1 下载 2 安装 3 新建站点 4 配置host phpStudy是一个PHP调试环境的程序集成包. 该程序包集成最新的 Apache+Nginx+LightTPD PHP MySQL+php ...
- 浅谈css样式之list-type
在我们的工作学习中,大多数人使用列表标签的时候总一般的选择是把list-type设置成none.不过可能很多人对于这个属性的细节并没有很深的了解.甚至会把list-type和list-type-sty ...
- tp5引用第三方类vendor或extend(多种方法)
extend 方法一:命名空间引入 我们只需要把自己的类库包目录放入EXTEND_PATH目录(默认为extend,可配置),就可以自动注册对应的命名空间,例如: 我们在extend目录下面新增一个l ...
- Reactive(3)5分钟理解 SpringBoot 响应式的核心-Reactor
目录 一.前言 二. Mono 与 Flux 构造器 三. 流计算 1. 缓冲 2. 过滤/提取 3. 转换 4. 合并 5. 合流 6. 累积 四.异常处理 五.线程调度 小结 参考阅读 一.前言 ...
- Redis 中的数据库
前面我们花了很多的时间介绍了 redis 中基本的数据结构,及其内部的实现情况,这些都是非常基础的东西,可能不经意间你就会用到他们,希望你花点时间了解一下. 接下来,我们将走近 redis 数据库,学 ...