使用Java解析JSON數(shù)據(jù)并提取特定字段的實現(xiàn)步驟(以提取mailNo為例)
1. 背景介紹
1.1 JSON簡介
JSON(JavaScript Object Notation)是一種輕量級的數(shù)據(jù)交換格式,易于人閱讀和編寫,同時也易于機器解析和生成。它基于JavaScript的一個子集,但獨立于語言,幾乎所有的編程語言都支持JSON格式的數(shù)據(jù)處理。
1.2 實際案例
假設我們有一個JSON數(shù)據(jù),其中包含多個訂單信息,每個訂單都有一個唯一的mailNo字段。我們的目標是從這個JSON數(shù)據(jù)中提取出所有的mailNo字段,并將其存儲在一個列表中。
2. 準備工作
2.1 環(huán)境搭建
為了解析JSON數(shù)據(jù),我們需要使用一個Java庫。在本文中,我們將使用Jackson庫,這是一個非常流行的JSON處理庫,功能強大且易于使用。
2.1.1 添加依賴
如果你使用Maven構建項目,可以在pom.xml中添加以下依賴:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
如果你使用Gradle構建項目,可以在build.gradle中添加以下依賴:
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.3'
2.2 JSON數(shù)據(jù)結構
以下是我們將要處理的JSON數(shù)據(jù)的示例:
{
"msg": "操作成功!",
"code": "ok",
"result": {
"records": [
{
"merchantId": 0,
"organId": 0,
"creatorId": 0,
"creatorTruename": "",
"creatorUsername": "",
"organName": "",
"merchantTruename": "",
"merchantUsername": "",
"consigneeName": "突然的自我",
"consigneePhone": "153****7763",
"consigneeTelPhone": "*******7763",
"consigneeCompany": "",
"consigneeProvinceId": 0,
"consigneeProvince": "四川省",
"consigneeCityId": 0,
"consigneeCity": "綿陽市",
"consigneeDistrictId": 0,
"consigneeDistrict": "涪城區(qū)",
"consigneeAddress": "四川省****靚妍美妝",
"sendProvinceId": 0,
"sendProvince": "貴州省",
"sendCityId": 0,
"sendCity": "遵義市",
"sendDistrictId": 0,
"sendDistrict": "紅花崗區(qū)",
"senderName": "夜郎酒業(yè)",
"senderCompany": "",
"senderPhone": "155****7773",
"senderTelPhone": "*******7773",
"senderDetailAddress": "貴州省****中轉倉庫",
"mailNo": "SF3150804745040",
"subWaybillNoList": [],
"waybillStatus": 50,
"printSerialNumber": "",
"printStatus": 0,
"printNumber": 0,
"orderValueAddedVo": null,
"addedServiceCodes": [],
"callFlag": "",
"consignment": "夜郎醬宏圖純糧食大曲坤沙酒醬香型白酒禮盒裝53度500ml",
"consignmentNumber": 2,
"parcelWeight": "2.0",
"parcelWeightUnit": "2.0",
"parcelQuantity": 1,
"meterageWeightQty": "2.0",
"realWeightQty": "2.0",
"orderNo": "6938567762089547327|2kBWWcmNUwX2SZ",
"originalOrderNo": "",
"signBackNo": "",
"remark": "",
"orderCreateTime": "2025-01-10 15:29:41",
"waybillCreateTime": "2025-01-10 15:29:41",
"printTime": null,
"isShowExcludeFile": 0,
"freight": "1000.0",
"referenceCost": "",
"waybillNums": 1,
"status": "",
"exceptionFlag": 0,
"promiseTime": "",
"receiveTime": "2025-01-10 16:16:55",
"signTime": "2025-01-11 14:13:40",
"latestRouteInfo": "您的快件已由本人簽收,如有疑問請電聯(lián)快遞員【楊發(fā)斌,電話:13330891935】。您的體驗對于我們至關重要,如果您對我們的服務有任何的想法和建議,請隨時聯(lián)系我們,我們一定用心傾聽,全力改進,不辜負您的信任與支持。",
"latestOpCode": "80",
"expressProductId": 0,
"expressProductCode": "B1",
"expressProductName": "電商標快",
"expressPayMethod": 1,
"expressPayMethodName": "寄付月結",
"monthlyCard": "052***6969",
"orderType": 1,
"customField1": "",
"customField2": "",
"customField3": "",
"amount": "",
"collectionAmount": "",
"collectingNumber": "",
"oldCollectingMoney": "",
"exceptionCode": "",
"orderId": 0,
"waybillId": 0,
"isUpdateWaybill": 0,
"mergeOrderType": 2,
"isDraft": null,
"failCause": "",
"orderCrossBorderGoodsVoList": [
{
"productCode": "",
"productName": "夜郎醬宏圖純糧食大曲坤沙酒醬香型白酒禮盒裝53度500ml",
"price": 0.0,
"currencyCode": "",
"currencyName": "",
"unit": "",
"productNumber": 2.0,
"orderType": 0,
"countryOfOrigin": ""
}
],
"gangAoTaiExtVo": null,
"swFlag": 0,
"sendStatus": 1,
"sendStatusName": "獲取成功",
"expressCompanyMerchantId": 0,
"totalLength": 41.0,
"totalHeight": 1.0,
"totalWidth": 1.0,
"addedValueServiceInfo": {
"insuredType": 0,
"insuredPrice": null,
"collectingMoney": null,
"collectingMonthlyCard": "",
"signBackName": [],
"signBackNo": "",
"signBackWaybillStatus": 0,
"signBackOrderType": [],
"signBackRouteList": [],
"isSignBack": 0,
"signBackRemark": "",
"signBackNum": 0,
"thirdSignBack": 0,
"thirdConsigneeName": "",
"thirdConsigneePhone": "",
"thirdConsigneeTel": "",
"thirdConsigneeProvince": "",
"thirdConsigneeCity": "",
"thirdConsigneeArea": "",
"thirdConsigneeAddress": "",
"thirdConsigneeCompany": "",
"takePhotoBack": [],
"takePhotoNum": 0,
"takePhotoBackRemark": "",
"timingSendInformType": 0,
"timingSendInformDate": "",
"timingSendInformTime": "",
"electronicBack": 0,
"secretKeyAuthentication": "",
"secretKeyAuthenticationValue": "",
"woodenPackage": 0,
"woodenPackageInfo": "",
"readySend": 0,
"woodenRemovalNum": 0,
"packagingService": "",
"packagingMaterialVos": [],
"overweightService": 0,
"timingInsured": 0,
"freshService": 0,
"homeDeliveryExt": [],
"installationType": 0,
"installationContent": [],
"selfPickup": 0,
"exhibitionService": 0,
"exhibitionExitService": 0,
"addedServiceCodes": [],
"customDeliveryTime": ""
},
"signBackInfo": "",
"signBackNum": "",
"pickupAppointTime": "",
"consValueCurrencyCode": "",
"exportDeclarationMethod": "",
"customsBatchs": "",
"taxAccount": "",
"taxPaymentType": "",
"custReferenceNo": "",
"isSignBack": 0,
"signBackRemark": "",
"cancelTime": null
},
{
"merchantId": 0,
"organId": 0,
"creatorId": 0,
"creatorTruename": "",
"creatorUsername": "",
"organName": "",
"merchantTruename": "",
"merchantUsername": "",
"consigneeName": "**",
"consigneePhone": "157*********2541",
"consigneeTelPhone": "************2541",
"consigneeCompany": "",
"consigneeProvinceId": 0,
"consigneeProvince": "貴州省",
"consigneeCityId": 0,
"consigneeCity": "貴陽市",
"consigneeDistrictId": 0,
"consigneeDistrict": "修文縣",
"consigneeAddress": "貴州省****獸霸鞋店",
"sendProvinceId": 0,
"sendProvince": "貴州省",
"sendCityId": 0,
"sendCity": "遵義市",
"sendDistrictId": 0,
"sendDistrict": "紅花崗區(qū)",
"senderName": "小包",
"senderCompany": "",
"senderPhone": "155****1949",
"senderTelPhone": "*******1949",
"senderDetailAddress": "貴州省****菜鳥驛站",
"mailNo": "SF3150809345059",
"subWaybillNoList": [],
"waybillStatus": 50,
"printSerialNumber": "",
"printStatus": 0,
"printNumber": 0,
"orderValueAddedVo": null,
"addedServiceCodes": [],
"callFlag": "",
"consignment": "2024男鞋男士舒適時尚百搭黑色41",
"consignmentNumber": 1,
"parcelWeight": "2.5",
"parcelWeightUnit": "2.5",
"parcelQuantity": 1,
"meterageWeightQty": "2.5",
"realWeightQty": "2.5",
"orderNo": "6938535195456968508|6780cc24dd560100",
"originalOrderNo": "",
"signBackNo": "",
"remark": "驢孤品42碼",
"orderCreateTime": "2025-01-10 15:28:59",
"waybillCreateTime": "2025-01-10 15:28:59",
"printTime": null,
"isShowExcludeFile": 0,
"freight": "1000.0",
"referenceCost": "",
"waybillNums": 1,
"status": "",
"exceptionFlag": 0,
"promiseTime": "",
"receiveTime": "2025-01-10 18:42:03",
"signTime": "2025-01-11 09:40:32",
"latestRouteInfo": "您的快件已由本人簽收,如有疑問請電聯(lián)快遞員【王昭發(fā),電話:18143512989】。您的體驗對于我們至關重要,如果您對我們的服務有任何的想法和建議,請隨時聯(lián)系我們,我們一定用心傾聽,全力改進,不辜負您的信任與支持。",
"latestOpCode": "80",
"expressProductId": 0,
"expressProductCode": "B1",
"expressProductName": "電商標快",
"expressPayMethod": 1,
"expressPayMethodName": "寄付月結",
"monthlyCard": "052***6969",
"orderType": 1,
"customField1": "",
"customField2": "",
"customField3": "",
"amount": "",
"collectionAmount": "",
"collectingNumber": "",
"oldCollectingMoney": "",
"exceptionCode": "",
"orderId": 0,
"waybillId": 0,
"isUpdateWaybill": 0,
"mergeOrderType": 2,
"isDraft": null,
"failCause": "",
"orderCrossBorderGoodsVoList": [
{
"productCode": "",
"productName": "2024男鞋男士舒適時尚百搭黑色41",
"price": 0.0,
"currencyCode": "",
"currencyName": "",
"unit": "",
"productNumber": 1.0,
"orderType": 0,
"countryOfOrigin": ""
}
],
"gangAoTaiExtVo": null,
"swFlag": 0,
"sendStatus": 1,
"sendStatusName": "獲取成功",
"expressCompanyMerchantId": 0,
"totalLength": 0.0,
"totalHeight": 0.0,
"totalWidth": 0.0,
"addedValueServiceInfo": {
"insuredType": 0,
"insuredPrice": null,
"collectingMoney": null,
"collectingMonthlyCard": "",
"signBackName": [],
"signBackNo": "",
"signBackWaybillStatus": 0,
"signBackOrderType": [],
"signBackRouteList": [],
"isSignBack": 0,
"signBackRemark": "",
"signBackNum": 0,
"thirdSignBack": 0,
"thirdConsigneeName": "",
"thirdConsigneePhone": "",
"thirdConsigneeTel": "",
"thirdConsigneeProvince": "",
"thirdConsigneeCity": "",
"thirdConsigneeArea": "",
"thirdConsigneeAddress": "",
"thirdConsigneeCompany": "",
"takePhotoBack": [],
"takePhotoNum": 0,
"takePhotoBackRemark": "",
"timingSendInformType": 0,
"timingSendInformDate": "",
"timingSendInformTime": "",
"electronicBack": 0,
"secretKeyAuthentication": "",
"secretKeyAuthenticationValue": "",
"woodenPackage": 0,
"woodenPackageInfo": "",
"readySend": 0,
"woodenRemovalNum": 0,
"packagingService": "",
"packagingMaterialVos": [],
"overweightService": 0,
"timingInsured": 0,
"freshService": 0,
"homeDeliveryExt": [],
"installationType": 0,
"installationContent": [],
"selfPickup": 0,
"exhibitionService": 0,
"exhibitionExitService": 0,
"addedServiceCodes": [],
"customDeliveryTime": ""
},
"signBackInfo": "",
"signBackNum": "",
"pickupAppointTime": "",
"consValueCurrencyCode": "",
"exportDeclarationMethod": "",
"customsBatchs": "",
"taxAccount": "",
"taxPaymentType": "",
"custReferenceNo": "",
"isSignBack": 0,
"signBackRemark": "",
"cancelTime": null
}
],
"total": 5167,
"size": 1000,
"current": 1,
"orders": [],
"searchCount": true,
"pages": 6
}
}
3. 實現(xiàn)步驟
3.1 創(chuàng)建Java類
我們將創(chuàng)建一個名為MailNoExtractor的Java類,用于解析JSON數(shù)據(jù)并提取mailNo字段。
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MailNoExtractor {
public static List<String> extractMailNos(String json) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(json);
JsonNode recordsNode = rootNode.path("result").path("records");
List<String> mailNos = new ArrayList<>();
for (JsonNode record : recordsNode) {
String mailNo = record.path("mailNo").asText();
mailNos.add(mailNo);
}
return mailNos;
}
public static void main(String[] args) {
String json = "{\"msg\":\"操作成功!\",\"code\":\"ok\",\"result\":{\"records\":[{\"merchantId\":0,\"organId\":0,\"creatorId\":0,\"creatorTruename\":\"\",\"creatorUsername\":\"\",\"organName\":\"\",\"merchantTruename\":\"\",\"merchantUsername\":\"\",\"consigneeName\":\"突然的自我\",\"consigneePhone\":\"153****7763\",\"consigneeTelPhone\":\"*******7763\",\"consigneeCompany\":\"\",\"consigneeProvinceId\":0,\"consigneeProvince\":\"四川省\",\"consigneeCityId\":0,\"consigneeCity\":\"綿陽市\(zhòng)",\"consigneeDistrictId\":0,\"consigneeDistrict\":\"涪城區(qū)\",\"consigneeAddress\":\"四川省****靚妍美妝\",\"sendProvinceId\":0,\"sendProvince\":\"貴州省\",\"sendCityId\":0,\"sendCity\":\"遵義市\(zhòng)",\"sendDistrictId\":0,\"sendDistrict\":\"紅花崗區(qū)\",\"senderName\":\"夜郎酒業(yè)\",\"senderCompany\":\"\",\"senderPhone\":\"155****7773\",\"senderTelPhone\":\"*******7773\",\"senderDetailAddress\":\"貴州省****中轉倉庫\",\"mailNo\":\"SF3150804745040\",\"subWaybillNoList\":[],\"waybillStatus\":50,\"printSerialNumber\":\"\",\"printStatus\":0,\"printNumber\":0,\"orderValueAddedVo\":null,\"addedServiceCodes\":[],\"callFlag\":\"\",\"consignment\":\"夜郎醬宏圖純糧食大曲坤沙酒醬香型白酒禮盒裝53度500ml\",\"consignmentNumber\":2,\"parcelWeight\":\"2.0\",\"parcelWeightUnit\":\"2.0\",\"parcelQuantity\":1,\"meterageWeightQty\":\"2.0\",\"realWeightQty\":\"2.0\",\"orderNo\":\"6938567762089547327|2kBWWcmNUwX2SZ\",\"originalOrderNo\":\"\",\"signBackNo\":\"\",\"remark\":\"\",\"orderCreateTime\":\"2025-01-10 15:29:41\",\"waybillCreateTime\":\"2025-01-10 15:29:41\",\"printTime\":null,\"isShowExcludeFile\":0,\"freight\":\"1000.0\",\"referenceCost\":\"\",\"waybillNums\":1,\"status\":\"\",\"exceptionFlag\":0,\"promiseTime\":\"\",\"receiveTime\":\"2025-01-10 16:16:55\",\"signTime\":\"2025-01-11 14:13:40\",\"latestRouteInfo\":\"您的快件已由本人簽收,如有疑問請電聯(lián)快遞員【楊發(fā)斌,電話:13330891935】。您的體驗對于我們至關重要,如果您對我們的服務有任何的想法和建議,請隨時聯(lián)系我們,我們一定用心傾聽,全力改進,不辜負您的信任與支持。\",\"latestOpCode\":\"80\",\"expressProductId\":0,\"expressProductCode\":\"B1\",\"expressProductName\":\"電商標快\",\"expressPayMethod\":1,\"expressPayMethodName\":\"寄付月結\",\"monthlyCard\":\"052***6969\",\"orderType\":1,\"customField1\":\"\",\"customField2\":\"\",\"customField3\":\"\",\"amount\":\"\",\"collectionAmount\":\"\",\"collectingNumber\":\"\",\"oldCollectingMoney\":\"\",\"exceptionCode\":\"\",\"orderId\":0,\"waybillId\":0,\"isUpdateWaybill\":0,\"mergeOrderType\":2,\"isDraft\":null,\"failCause\":\"\",\"orderCrossBorderGoodsVoList\":[{\"productCode\":\"\",\"productName\":\"夜郎醬宏圖純糧食大曲坤沙酒醬香型白酒禮盒裝53度500ml\",\"price\":0.0,\"currencyCode\":\"\",\"currencyName\":\"\",\"unit\":\"\",\"productNumber\":2.0,\"orderType\":0,\"countryOfOrigin\":\"\"}],\"gangAoTaiExtVo\":null,\"swFlag\":0,\"sendStatus\":1,\"sendStatusName\":\"獲取成功\",\"expressCompanyMerchantId\":0,\"totalLength\":41.0,\"totalHeight\":1.0,\"totalWidth\":1.0,\"addedValueServiceInfo\":{\"insuredType\":0,\"insuredPrice\":null,\"collectingMoney\":null,\"collectingMonthlyCard\":\"\",\"signBackName\":[],\"signBackNo\":\"\",\"signBackWaybillStatus\":0,\"signBackOrderType\":[],\"signBackRouteList\":[],\"isSignBack\":0,\"signBackRemark\":\"\",\"signBackNum\":0,\"thirdSignBack\":0,\"thirdConsigneeName\":\"\",\"thirdConsigneePhone\":\"\",\"thirdConsigneeTel\":\"\",\"thirdConsigneeProvince\":\"\",\"thirdConsigneeCity\":\"\",\"thirdConsigneeArea\":\"\",\"thirdConsigneeAddress\":\"\",\"thirdConsigneeCompany\":\"\",\"takePhotoBack\":[],\"takePhotoNum\":0,\"takePhotoBackRemark\":\"\",\"timingSendInformType\":0,\"timingSendInformDate\":\"\",\"timingSendInformTime\":\"\",\"electronicBack\":0,\"secretKeyAuthentication\":\"\",\"secretKeyAuthenticationValue\":\"\",\"woodenPackage\":0,\"woodenPackageInfo\":\"\",\"readySend\":0,\"woodenRemovalNum\":0,\"packagingService\":\"\",\"packagingMaterialVos\":[],\"overweightService\":0,\"timingInsured\":0,\"freshService\":0,\"homeDeliveryExt\":[],\"installationType\":0,\"installationContent\":[],\"selfPickup\":0,\"exhibitionService\":0,\"exhibitionExitService\":0,\"addedServiceCodes\":[],\"customDeliveryTime\":\"\"},\"signBackInfo\":\"\",\"signBackNum\":\"\",\"pickupAppointTime\":\"\",\"consValueCurrencyCode\":\"\",\"exportDeclarationMethod\":\"\",\"customsBatchs\":\"\",\"taxAccount\":\"\",\"taxPaymentType\":\"\",\"custReferenceNo\":\"\",\"isSignBack\":0,\"signBackRemark\":\"\",\"cancelTime\":null},{\"merchantId\":0,\"organId\":0,\"creatorId\":0,\"creatorTruename\":\"\",\"creatorUsername\":\"\",\"organName\":\"\",\"merchantTruename\":\"\",\"merchantUsername\":\"\",\"consigneeName\":\"**\",\"consigneePhone\":\"157*********2541\",\"consigneeTelPhone\":\"************2541\",\"consigneeCompany\":\"\",\"consigneeProvinceId\":0,\"consigneeProvince\":\"貴州省\",\"consigneeCityId\":0,\"consigneeCity\":\"貴陽市\(zhòng)",\"consigneeDistrictId\":0,\"consigneeDistrict\":\"修文縣\",\"consigneeAddress\":\"貴州省****獸霸鞋店\",\"sendProvinceId\":0,\"sendProvince\":\"貴州省\",\"sendCityId\":0,\"sendCity\":\"遵義市\(zhòng)",\"sendDistrictId\":0,\"sendDistrict\":\"紅花崗區(qū)\",\"senderName\":\"小包\",\"senderCompany\":\"\",\"senderPhone\":\"155****1949\",\"senderTelPhone\":\"*******1949\",\"senderDetailAddress\":\"貴州省****菜鳥驛站\",\"mailNo\":\"SF3150809345059\",\"subWaybillNoList\":[],\"waybillStatus\":50,\"printSerialNumber\":\"\",\"printStatus\":0,\"printNumber\":0,\"orderValueAddedVo\":null,\"addedServiceCodes\":[],\"callFlag\":\"\",\"consignment\":\"2024男鞋男士舒適時尚百搭黑色41\",\"consignmentNumber\":1,\"parcelWeight\":\"2.5\",\"parcelWeightUnit\":\"2.5\",\"parcelQuantity\":1,\"meterageWeightQty\":\"2.5\",\"realWeightQty\":\"2.5\",\"orderNo\":\"6938535195456968508|6780cc24dd560100\",\"originalOrderNo\":\"\",\"signBackNo\":\"\",\"remark\":\"驢孤品42碼\",\"orderCreateTime\":\"2025-01-10 15:28:59\",\"waybillCreateTime\":\"2025-01-10 15:28:59\",\"printTime\":null,\"isShowExcludeFile\":0,\"freight\":\"1000.0\",\"referenceCost\":\"\",\"waybillNums\":1,\"status\":\"\",\"exceptionFlag\":0,\"promiseTime\":\"\",\"receiveTime\":\"2025-01-10 18:42:03\",\"signTime\":\"2025-01-11 09:40:32\",\"latestRouteInfo\":\"您的快件已由本人簽收,如有疑問請電聯(lián)快遞員【王昭發(fā),電話:18143512989】。您的體驗對于我們至關重要,如果您對我們的服務有任何的想法和建議,請隨時聯(lián)系我們,我們一定用心傾聽,全力改進,不辜負您的信任與支持。\",\"latestOpCode\":\"80\",\"expressProductId\":0,\"expressProductCode\":\"B1\",\"expressProductName\":\"電商標快\",\"expressPayMethod\":1,\"expressPayMethodName\":\"寄付月結\",\"monthlyCard\":\"052***6969\",\"orderType\":1,\"customField1\":\"\",\"customField2\":\"\",\"customField3\":\"\",\"amount\":\"\",\"collectionAmount\":\"\",\"collectingNumber\":\"\",\"oldCollectingMoney\":\"\",\"exceptionCode\":\"\",\"orderId\":0,\"waybillId\":0,\"isUpdateWaybill\":0,\"mergeOrderType\":2,\"isDraft\":null,\"failCause\":\"\",\"orderCrossBorderGoodsVoList\":[{\"productCode\":\"\",\"productName\":\"2024男鞋男士舒適時尚百搭黑色41\",\"price\":0.0,\"currencyCode\":\"\",\"currencyName\":\"\",\"unit\":\"\",\"productNumber\":1.0,\"orderType\":0,\"countryOfOrigin\":\"\"}],\"gangAoTaiExtVo\":null,\"swFlag\":0,\"sendStatus\":1,\"sendStatusName\":\"獲取成功\",\"expressCompanyMerchantId\":0,\"totalLength\":0.0,\"totalHeight\":0.0,\"totalWidth\":0.0,\"addedValueServiceInfo\":{\"insuredType\":0,\"insuredPrice\":null,\"collectingMoney\":null,\"collectingMonthlyCard\":\"\",\"signBackName\":[],\"signBackNo\":\"\",\"signBackWaybillStatus\":0,\"signBackOrderType\":[],\"signBackRouteList\":[],\"isSignBack\":0,\"signBackRemark\":\"\",\"signBackNum\":0,\"thirdSignBack\":0,\"thirdConsigneeName\":\"\",\"thirdConsigneePhone\":\"\",\"thirdConsigneeTel\":\"\",\"thirdConsigneeProvince\":\"\",\"thirdConsigneeCity\":\"\",\"thirdConsigneeArea\":\"\",\"thirdConsigneeAddress\":\"\",\"thirdConsigneeCompany\":\"\",\"takePhotoBack\":[],\"takePhotoNum\":0,\"takePhotoBackRemark\":\"\",\"timingSendInformType\":0,\"timingSendInformDate\":\"\",\"timingSendInformTime\":\"\",\"electronicBack\":0,\"secretKeyAuthentication\":\"\",\"secretKeyAuthenticationValue\":\"\",\"woodenPackage\":0,\"woodenPackageInfo\":\"\",\"readySend\":0,\"woodenRemovalNum\":0,\"packagingService\":\"\",\"packagingMaterialVos\":[],\"overweightService\":0,\"timingInsured\":0,\"freshService\":0,\"homeDeliveryExt\":[],\"installationType\":0,\"installationContent\":[],\"selfPickup\":0,\"exhibitionService\":0,\"exhibitionExitService\":0,\"addedServiceCodes\":[],\"customDeliveryTime\":\"\"},\"signBackInfo\":\"\",\"signBackNum\":\"\",\"pickupAppointTime\":\"\",\"consValueCurrencyCode\":\"\",\"exportDeclarationMethod\":\"\",\"customsBatchs\":\"\",\"taxAccount\":\"\",\"taxPaymentType\":\"\",\"custReferenceNo\":\"\",\"isSignBack\":0,\"signBackRemark\":\"\",\"cancelTime\":null}],\"total\":5167,\"size\":1000,\"current\":1,\"orders\":[],\"searchCount\":true,\"pages\":6}}";
try {
List<String> mailNos = extractMailNos(json);
for (String mailNo : mailNos) {
System.out.println(mailNo);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.2 代碼解析
3.2.1 extractMailNos 方法
- 功能:該方法接收一個JSON字符串作為輸入,使用Jackson庫解析JSON,并提取出所有
mailNo字段的值,返回一個包含所有mailNo的列表。 - 步驟:
- 使用
ObjectMapper將JSON字符串解析為JsonNode對象。 - 通過
path方法獲取result節(jié)點下的records數(shù)組。 - 遍歷
records數(shù)組,提取每個記錄中的mailNo字段,并將其添加到列表中。
- 使用
3.2.2 main 方法
- 功能:這是一個簡單的測試方法,展示了如何使用
extractMailNos方法來提取mailNo并打印出來。 - 步驟:
- 定義一個JSON字符串。
- 調用
extractMailNos方法提取mailNo。 - 遍歷并打印所有
mailNo。
3.3 運行結果
運行上述代碼后,輸出結果如下:
SF3150804745040
SF3150809345059
4. 總結
本文詳細介紹了如何使用Java解析JSON數(shù)據(jù)并提取特定字段。通過使用Jackson庫,我們可以輕松地處理復雜的JSON數(shù)據(jù)結構,并提取出我們需要的字段。這個工具類可以根據(jù)實際需求進一步擴展和優(yōu)化,例如添加異常處理、支持更多字段的提取等。
希望本文對你理解和使用Java處理JSON數(shù)據(jù)有所幫助!
到此這篇關于使用Java解析JSON數(shù)據(jù)并提取特定字段的實現(xiàn)步驟(以提取mailNo為例)的文章就介紹到這了,更多相關Java解析JSON數(shù)據(jù)并提取字段內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot整合FastDFS中間件實現(xiàn)文件分布管理
FastDFS是一個開源的輕量級分布式文件系統(tǒng),它對文件進行管理,功能包括:文件存儲、文件同步、文件上傳、文件下載等,解決了大容量存儲和負載均衡的問題,本文介紹了SpringBoot整合FastDFS中間件實現(xiàn)文件分布管理,需要的朋友可以參考下2024-08-08
Java在Map轉Json字符串時出現(xiàn)"\"轉義字符的解決辦法
當一個Map被轉成Json字符串后,被添加到另一個Map中,會出現(xiàn)被加上“\”轉義字符的情況,這個時候該如何解決呢,下面就來和小編一起了解一下2023-07-07
詳解spring cloud分布式整合zipkin的鏈路跟蹤
這篇文章主要介紹了詳解spring cloud分布式整合zipkin的鏈路跟蹤,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
SpringCloud?LoadBalancerClient?負載均衡原理解析
LoadBalancerClient?是?SpringCloud?提供的一種負載均衡客戶端,Ribbon?負載均衡組件內部也是集成了?LoadBalancerClient?來實現(xiàn)負載均衡,本文給大家深入解析?LoadBalancerClient?接口源碼,感興趣的朋友跟隨小編一起看看吧2022-02-02
Java Optional解決空指針異??偨Y(java 8 功能)
這篇文章主要介紹了Java Optional解決空指針異??偨Y(java 8 功能),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
SpringBoot整合mybatis-generator-maven-plugin的方法
這篇文章主要介紹了SpringBoot整合mybatis-generator-maven-plugin,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11

