使用Java解析JSON數(shù)據(jù)并提取特定字段的實(shí)現(xiàn)步驟(以提取mailNo為例)
1. 背景介紹
1.1 JSON簡(jiǎn)介
JSON(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,易于人閱讀和編寫,同時(shí)也易于機(jī)器解析和生成。它基于JavaScript的一個(gè)子集,但獨(dú)立于語(yǔ)言,幾乎所有的編程語(yǔ)言都支持JSON格式的數(shù)據(jù)處理。
1.2 實(shí)際案例
假設(shè)我們有一個(gè)JSON數(shù)據(jù),其中包含多個(gè)訂單信息,每個(gè)訂單都有一個(gè)唯一的mailNo字段。我們的目標(biāo)是從這個(gè)JSON數(shù)據(jù)中提取出所有的mailNo字段,并將其存儲(chǔ)在一個(gè)列表中。
2. 準(zhǔn)備工作
2.1 環(huán)境搭建
為了解析JSON數(shù)據(jù),我們需要使用一個(gè)Java庫(kù)。在本文中,我們將使用Jackson庫(kù),這是一個(gè)非常流行的JSON處理庫(kù),功能強(qiáng)大且易于使用。
2.1.1 添加依賴
如果你使用Maven構(gòu)建項(xiàng)目,可以在pom.xml中添加以下依賴:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
如果你使用Gradle構(gòu)建項(xiàng)目,可以在build.gradle中添加以下依賴:
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.3'
2.2 JSON數(shù)據(jù)結(jié)構(gòu)
以下是我們將要處理的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": "綿陽(yáng)市",
"consigneeDistrictId": 0,
"consigneeDistrict": "涪城區(qū)",
"consigneeAddress": "四川省****靚妍美妝",
"sendProvinceId": 0,
"sendProvince": "貴州省",
"sendCityId": 0,
"sendCity": "遵義市",
"sendDistrictId": 0,
"sendDistrict": "紅花崗區(qū)",
"senderName": "夜郎酒業(yè)",
"senderCompany": "",
"senderPhone": "155****7773",
"senderTelPhone": "*******7773",
"senderDetailAddress": "貴州省****中轉(zhuǎn)倉(cāng)庫(kù)",
"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": "您的快件已由本人簽收,如有疑問(wèn)請(qǐng)電聯(lián)快遞員【楊發(fā)斌,電話:13330891935】。您的體驗(yàn)對(duì)于我們至關(guān)重要,如果您對(duì)我們的服務(wù)有任何的想法和建議,請(qǐng)隨時(shí)聯(lián)系我們,我們一定用心傾聽,全力改進(jìn),不辜負(fù)您的信任與支持。",
"latestOpCode": "80",
"expressProductId": 0,
"expressProductCode": "B1",
"expressProductName": "電商標(biāo)快",
"expressPayMethod": 1,
"expressPayMethodName": "寄付月結(jié)",
"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": "貴陽(yáng)市",
"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男鞋男士舒適時(shí)尚百搭黑色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": "您的快件已由本人簽收,如有疑問(wèn)請(qǐng)電聯(lián)快遞員【王昭發(fā),電話:18143512989】。您的體驗(yàn)對(duì)于我們至關(guān)重要,如果您對(duì)我們的服務(wù)有任何的想法和建議,請(qǐng)隨時(shí)聯(lián)系我們,我們一定用心傾聽,全力改進(jìn),不辜負(fù)您的信任與支持。",
"latestOpCode": "80",
"expressProductId": 0,
"expressProductCode": "B1",
"expressProductName": "電商標(biāo)快",
"expressPayMethod": 1,
"expressPayMethodName": "寄付月結(jié)",
"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男鞋男士舒適時(shí)尚百搭黑色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. 實(shí)現(xiàn)步驟
3.1 創(chuàng)建Java類
我們將創(chuàng)建一個(gè)名為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\":\"綿陽(yáng)市\(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\":\"貴州省****中轉(zhuǎn)倉(cāng)庫(kù)\",\"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\":\"您的快件已由本人簽收,如有疑問(wèn)請(qǐng)電聯(lián)快遞員【楊發(fā)斌,電話:13330891935】。您的體驗(yàn)對(duì)于我們至關(guān)重要,如果您對(duì)我們的服務(wù)有任何的想法和建議,請(qǐng)隨時(shí)聯(lián)系我們,我們一定用心傾聽,全力改進(jìn),不辜負(fù)您的信任與支持。\",\"latestOpCode\":\"80\",\"expressProductId\":0,\"expressProductCode\":\"B1\",\"expressProductName\":\"電商標(biāo)快\",\"expressPayMethod\":1,\"expressPayMethodName\":\"寄付月結(jié)\",\"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\":\"貴陽(yáng)市\(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男鞋男士舒適時(shí)尚百搭黑色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\":\"您的快件已由本人簽收,如有疑問(wèn)請(qǐng)電聯(lián)快遞員【王昭發(fā),電話:18143512989】。您的體驗(yàn)對(duì)于我們至關(guān)重要,如果您對(duì)我們的服務(wù)有任何的想法和建議,請(qǐng)隨時(shí)聯(lián)系我們,我們一定用心傾聽,全力改進(jìn),不辜負(fù)您的信任與支持。\",\"latestOpCode\":\"80\",\"expressProductId\":0,\"expressProductCode\":\"B1\",\"expressProductName\":\"電商標(biāo)快\",\"expressPayMethod\":1,\"expressPayMethodName\":\"寄付月結(jié)\",\"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男鞋男士舒適時(shí)尚百搭黑色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 方法
- 功能:該方法接收一個(gè)JSON字符串作為輸入,使用Jackson庫(kù)解析JSON,并提取出所有
mailNo字段的值,返回一個(gè)包含所有mailNo的列表。 - 步驟:
- 使用
ObjectMapper將JSON字符串解析為JsonNode對(duì)象。 - 通過(guò)
path方法獲取result節(jié)點(diǎn)下的records數(shù)組。 - 遍歷
records數(shù)組,提取每個(gè)記錄中的mailNo字段,并將其添加到列表中。
- 使用
3.2.2 main 方法
- 功能:這是一個(gè)簡(jiǎn)單的測(cè)試方法,展示了如何使用
extractMailNos方法來(lái)提取mailNo并打印出來(lái)。 - 步驟:
- 定義一個(gè)JSON字符串。
- 調(diào)用
extractMailNos方法提取mailNo。 - 遍歷并打印所有
mailNo。
3.3 運(yùn)行結(jié)果
運(yùn)行上述代碼后,輸出結(jié)果如下:
SF3150804745040
SF3150809345059
4. 總結(jié)
本文詳細(xì)介紹了如何使用Java解析JSON數(shù)據(jù)并提取特定字段。通過(guò)使用Jackson庫(kù),我們可以輕松地處理復(fù)雜的JSON數(shù)據(jù)結(jié)構(gòu),并提取出我們需要的字段。這個(gè)工具類可以根據(jù)實(shí)際需求進(jìn)一步擴(kuò)展和優(yōu)化,例如添加異常處理、支持更多字段的提取等。
希望本文對(duì)你理解和使用Java處理JSON數(shù)據(jù)有所幫助!
到此這篇關(guān)于使用Java解析JSON數(shù)據(jù)并提取特定字段的實(shí)現(xiàn)步驟(以提取mailNo為例)的文章就介紹到這了,更多相關(guān)Java解析JSON數(shù)據(jù)并提取字段內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA全量替換一次性解決舊項(xiàng)目并將所有文件換行符改為L(zhǎng)F問(wèn)題
這篇文章主要介紹了IDEA全量替換一次性解決舊項(xiàng)目并將所有文件換行符改為L(zhǎng)F問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下2019-05-05
SpringBoot整合GRPC微服務(wù)遠(yuǎn)程通信的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot整合GRPC微服務(wù)遠(yuǎn)程通信的實(shí)現(xiàn)示例,包含gRPC的工作原理,以及如何在Spring Boot應(yīng)用中集成gRPC,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
Java Hibernate中的持久化類和實(shí)體類關(guān)系
Hibernate是一種Java對(duì)象關(guān)系映射框架,通過(guò)持久化類將Java對(duì)象映射到數(shù)據(jù)庫(kù)表中。持久化類需要實(shí)現(xiàn)無(wú)參構(gòu)造器、具有標(biāo)識(shí)屬性和使用注解或XML進(jìn)行映射。Hibernate通過(guò)Session來(lái)管理對(duì)象的狀態(tài),包括臨時(shí)狀態(tài)、持久化狀態(tài)和游離狀態(tài)2023-04-04
JavaWeb中Servlet的生命周期及線程安全問(wèn)題詳解
這篇文章主要介紹了JavaWeb中Servlet的生命周期及線程安全問(wèn)題詳解,Servlet?生命周期可被定義為從創(chuàng)建直到毀滅的整個(gè)過(guò)程,Servlet體系結(jié)構(gòu)是建立在Java多線程機(jī)制之上的,它的生命周期是由Web容器負(fù)責(zé)的,需要的朋友可以參考下2024-01-01

