大家好,欢迎来到IT知识分享网。
就很简单的一个问题折磨了整整一天,好在压抑的心情终于释放了,终于闲下来觉得是不是应该记录一下。
首先这个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和构造器我就不贴了,有点太占空间了)
1 import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 2 import java.io.Serializable; 3 import java.util.List; 4 5 @JsonIgnoreProperties(ignoreUnknown = true) 6 @SuppressWarnings("serial") 7 public class Result implements Serializable { 8 9 private String status; 10 private List<biz_result> biz_result; 11 private String error;
1 import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 2 import java.io.Serializable; 3 import java.util.List; 4 5 @JsonIgnoreProperties(ignoreUnknown = true) 6 @SuppressWarnings("serial") 7 public class biz_result implements Serializable { 8 private String PartyFunctionCode; 9 private String PartyIdentifier; 10 private String PartyName; 11 private List<ManifestInfo> ManifestInfo ; 12 private List<ContainerInfo> ContainerInfo ;
1 import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 2 import java.io.Serializable; 3 4 @JsonIgnoreProperties(ignoreUnknown = true) 5 @SuppressWarnings("serial") 6 public class ManifestInfo implements Serializable { 7 private String LadingBillNumber; 8 private String ShippingCompanyName; 9 private String VesselNameCode; 10 private String VesselName; 11 private String PlaceOfLoading; 12 private String PlaceOfDischarge; 13 private String VoyageNumber; 14 private String DirectionCode; 15 private String VesselCallNumber; 16 private String IMONumber;
1 import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 2 import java.io.Serializable; 3 import java.util.List; 4 5 @JsonIgnoreProperties(ignoreUnknown = true) 6 @SuppressWarnings("serial") 7 public class ContainerInfo implements Serializable { 8 9 private String ContainerNumber; 10 private String ContainerOperator; 11 private String SealNumber; 12 private String CodeOfSizeAndType; 13 private String ContainerLoadingStatusCode; 14 private String ContainerTareWeight; 15 private String TransportMeansType; 16 private String VehicleNumber; 17 private List<ContainerStatusInformation> ContainerStatusInformation ;
1 import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 2 import java.io.Serializable; 3 4 @JsonIgnoreProperties(ignoreUnknown = true) 5 @SuppressWarnings("serial") 6 public class ContainerStatusInformation implements Serializable { 7 8 private String ImExIdentifier; 9 private String TransportStatusCode; 10 private String TransportStatusNameEN; 11 private String DateOrTimeOrPeriod; 12 private String TimeZoneIdentifier; 13 private String PlaceLocationQualifier; 14 private String PlaceOrLocationIdentification; 15 private String PlaceOrLocation; 16 private String Remark; 17 private String Status; 18 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>
最终代码如下:
1 import net.sf.json.JSONArray; 2 import net.sf.json.JSONObject; 3 4 public class Test02 { 5 6 public static void main(String[]args){ 7 8 String json = "这是上面巨长的JSON"; 9 10 //得到json对象 11 JSONObject jsonObject = JSONObject.fromObject(json); 12 //获取其中的biz_result数组 13 JSONArray biz_result = jsonObject.getJSONArray("biz_result"); 14 //获取数组中的某位元素 15 String biz_resultString = biz_result.getString(0); 16 //将数组中的这个数组再次转换成json对象 17 JSONObject jsonObject1 = JSONObject.fromObject(biz_resultString); 18 //通过getString获取value 19 String manifestInfo = jsonObject1.getString("ManifestInfo"); 20 //去除首位中括号得到json格式 21 String substring = manifestInfo.substring(1); 22 String subString = substring.substring(0,substring.length()-1); 23 //得到json格式以后再转换成json对象 24 JSONObject jb3 = JSONObject.fromObject(subString); 25 //得到需要的值。 26 String vesselName = jb3.getString("VesselName"); 27 System.out.println(vesselName); 28 29 } 30 }
还是得养成写注释的习惯,要是不写注释贴上来,我自己都看着不舒服,最终得到了需要的数据:
COSCO YINGKOU
1234567.
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/34473.html