就很简单的一个问题折磨了整整一天,好在压抑的心情终于释放了,终于闲下来觉得是不是应该记录一下。

首先这个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的更多相关文章

  1. Java分享笔记:使用entrySet方法获取Map集合中的元素

    /*--------------------------------- 使用entrySet方法取出Map集合中的元素: ....该方法是将Map集合中key与value的关系存入到了Set集合中,这 ...

  2. Java分享笔记:使用keySet方法获取Map集合中的元素

    /*--------------------------- Map集合中利用keySet方法获取所有的元素值: ....keySet方法:将Map中的所有key值存入到Set集合中, ....利用Se ...

  3. Java如何接收前端传来的多层嵌套的复杂json串

    想看问题直接解决方式,直接拉到博文底部. Spring的controller在接收前端传参的时候如果参数使用@RequestBody标注的时候 @RequestBody 则会把前端参数转为JSON的形 ...

  4. Java使用实现面向对象编程:第七章集合框架的解读=>重中之重

    对于集合框架,是非常重要的知识,是程序员必须要知道的知识点. 但是我们为什么要引入集合框架呢? 我们之前用过数组存储数据,但是采用数组存储存在了很多的缺陷.而现在我们引用了集合框架,可以完全弥补了数组 ...

  5. DataTable/集合 转 Json

    前端用的jqueryUI框架获取json格式数据绑定显示表格. 后端通过WebService获取的数据是DataTable. 现将获取DataTable转Json,也支持将数据集合转Json. 一.项 ...

  6. python字典嵌套字典的情况下获取某个key的value

    最近在用python写接口的测试程序,期间用到解析字典获取某个key的value,由于多个接口返回的字典格式不是固定的并存在多层嵌套的情况.在字典的方法中也没有找到可直接达到目的的方法(也可能是我对字 ...

  7. Java 获取*.properties配置文件中的内容 ,常见的两种方法

    import java.io.InputStream; import java.util.Enumeration; import java.util.List; import java.util.Pr ...

  8. 【SpringBoot】 Java中如何封装Http请求,以及JSON多层嵌套解析

    前言 本文中的内容其实严格来说不算springboot里面的特性,属于JAVA基础,只是我在项目中遇到了,特归纳总结一下. HTTP请求封装 目前JAVA对于HTTP封装主要有三种方式: 1. JAV ...

  9. 【Java_Spring】java解析多层嵌套json字符串

    java解析多层嵌套json字符串    

  10. 多层嵌套的json数据

    很多时候我们见到的json数据都是多层嵌套的,就像下面这般: {"name":"桔子桑", "sex":"男", , & ...

随机推荐

  1. Chapter 07-Basic statistics(Part1 描述统计数据)

    在这一部分中,仍然使用mtcars(Motor Trend Car Road Tests)这一数据集,以及mpg(one mile per gallon), hp(horsepower), wt(we ...

  2. Scrapy持久化存储-爬取数据转义

    Scrapy持久化存储 爬虫爬取数据转义问题 使用这种格式,会自动帮我们转义 'insert into wen values(%s,%s)',(item['title'],item['content' ...

  3. PHP自动发红包代码示例

    <?php header('Content-type:text'); define("TOKEN", "weixin"); $wechatObj = ne ...

  4. 链式栈-C语言实现

    相对于顺序栈的空间有限,链式栈的操作则更加灵活 #include<stdio.h> #include<malloc.h> typedef int SElemType; //元素 ...

  5. 《Windows内核安全与驱动开发》 2.3 重要的数据结构

    <Windows内核安全与驱动开发>阅读笔记 -- 索引目录 <Windows内核安全与驱动开发> 2.3 重要的数据结构 一.驱动对象  Windows内核采用__的编程方式 ...

  6. 区块链学习笔记:D03 区块链在各行业领域的应用(一)

    今天主要是学习了区块链在金融和供应链领域的应用,重点体现了区块链多方参与.透明可信.防篡改防抵赖的技术优势 区块链的应用场景最早是在金融行业应用较多,后续逐步扩展到传统行业,如:供应链.政务服务.物联 ...

  7. 压缩感知重构算法之IRLS算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  8. 源码分析—ThreadPoolExecutor线程池三大问题及改进方案

    前言 在一次聚会中,我和一个腾讯大佬聊起了池化技术,提及到java的线程池实现问题,我说这个我懂啊,然后巴拉巴拉说了一大堆,然后腾讯大佬问我说,那你知道线程池有什么缺陷吗?我顿时哑口无言,甘拜下风,所 ...

  9. .net core 3.1简体中文语言包,英文只能提示变成中文!

    .net core 3.1简体中文语言包,英文只能提示变成中文!这个是我自己手动用谷歌翻译翻译的一批文档,已经把.net core3.1完全翻译了,由于是翻译器翻译,所以怕翻译有错漏,所以没有去掉英文 ...

  10. 最简单易懂的linux系统基础优化

    第10章 linux的系统优化 10.1 系统的版本,内核查看 10.1.1 系统版本的查看 [root@oldboy6666 ~]# cat /etc/redhat-release CentOS L ...