1. Background introduction
1.1 Introduction to JSON
JSON (JavaScript Object Notation) is a lightweight data exchange format that is easy to read and write by people, and is also easy to machine parse and generate. It is based on a subset of JavaScript, but is independent of the language, and almost all programming languages support data processing in JSON format.
1.2 Actual cases
Suppose we have a JSON data that contains multiple order information, each order has a unique mailNo field. Our goal is to extract all mailNo fields from this JSON data and store them in a list.
2. Preparation
2.1 Environment construction
In order to parse JSON data, we need to use a Java library. In this article, we will use the Jackson library, a very popular JSON processing library that is powerful and easy to use.
2.1.1 Add dependencies
If you build a project using Maven, you can add the following dependencies in it:
<dependency> <groupId></groupId> <artifactId>jackson-databind</artifactId> <version>2.13.3</version> </dependency>
If you build a project using Gradle, you canAdd the following dependencies to:
implementation ':jackson-databind:2.13.3'
2.2 JSON data structure
Here is an example of the JSON data we will be processing:
{ "msg": "The operation was successful!", "code": "ok", "result": { "records": [ { "merchantId": 0, "organId": 0, "creatorId": 0, "creatorTruename": "", "creatorUsername": "", "organName": "", "merchantTruename": "", "merchantUsername": "", "consigneeName": "Sudden ego", "consigneePhone": "153****7763", "consigneeTelPhone": "*******7763", "consigneeCompany": "", "consigneeProvinceId": 0, "consigneeProvince": "Sichuan Province", "consigneeCityId": 0, "consigneeCity": "Mianyang City", "consigneeDistrictId": 0, "consigneeDistrict": "Fucheng District", "consigneeAddress": "Sichuan Province****Beautiful makeup", "sendProvinceId": 0, "sendProvince": "Guizhou Province", "sendCityId": 0, "sendCity": "Zunyi City", "sendDistrictId": 0, "sendDistrict": "Honghuagang District", "senderName": "Yelang Wine Industry", "senderCompany": "", "senderPhone": "155****7773", "senderTelPhone": "*******7773", "senderDetailAddress": "Guizhou Province****Transfer warehouse", "mailNo": "SF3150804745040", "subWaybillNoList": [], "waybillStatus": 50, "printSerialNumber": "", "printStatus": 0, "printNumber": 0, "orderValueAddedVo": null, "addedServiceCodes": [], "callFlag": "", "consignment": "Yelang Jiang Hongtu Pure Grain Daqu Kunsha Liquor Sauce Flavor Liquor Gift Box Pack53Spend500ml", "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": "Your express delivery has been signed by me,If you have any questions, please contact the telecom courier.【Yang Fabin,Telephone:13330891935】。Your experience is crucial to us,If you have any ideas and suggestions about our services,Please feel free to contact us,We must listen carefully,Make every effort to improve,Live up to your trust and support。", "latestOpCode": "80", "expressProductId": 0, "expressProductCode": "B1", "expressProductName": "Fast e-mark", "expressPayMethod": 1, "expressPayMethodName": "Send monthly payment", "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": "Yelang Jiang Hongtu Pure Grain Daqu Kunsha Liquor Sauce Flavor Liquor Gift Box Pack53Spend500ml", "price": 0.0, "currencyCode": "", "currencyName": "", "unit": "", "productNumber": 2.0, "orderType": 0, "countryOfOrigin": "" } ], "gangAoTaiExtVo": null, "swFlag": 0, "sendStatus": 1, "sendStatusName": "Get Successful", "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": "Guizhou Province", "consigneeCityId": 0, "consigneeCity": "Guiyang City", "consigneeDistrictId": 0, "consigneeDistrict": "Xiuwen County", "consigneeAddress": "Guizhou Province****Baba Shoe Store", "sendProvinceId": 0, "sendProvince": "Guizhou Province", "sendCityId": 0, "sendCity": "Zunyi City", "sendDistrictId": 0, "sendDistrict": "Honghuagang District", "senderName": "packet", "senderCompany": "", "senderPhone": "155****1949", "senderTelPhone": "*******1949", "senderDetailAddress": "Guizhou Province****Cainiao Station", "mailNo": "SF3150809345059", "subWaybillNoList": [], "waybillStatus": 50, "printSerialNumber": "", "printStatus": 0, "printNumber": 0, "orderValueAddedVo": null, "addedServiceCodes": [], "callFlag": "", "consignment": "2024Men's shoes for men's comfortable and fashionable versatile black41", "consignmentNumber": 1, "parcelWeight": "2.5", "parcelWeightUnit": "2.5", "parcelQuantity": 1, "meterageWeightQty": "2.5", "realWeightQty": "2.5", "orderNo": "6938535195456968508|6780cc24dd560100", "originalOrderNo": "", "signBackNo": "", "remark": "Donkey orphan42code", "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": "Your express delivery has been signed by me,If you have any questions, please contact the telecom courier.【Wang Zhaofa,Telephone:18143512989】。Your experience is crucial to us,If you have any ideas and suggestions about our services,Please feel free to contact us,We must listen carefully,Make every effort to improve,Live up to your trust and support。", "latestOpCode": "80", "expressProductId": 0, "expressProductCode": "B1", "expressProductName": "Fast e-mark", "expressPayMethod": 1, "expressPayMethodName": "Send monthly payment", "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": "2024Men's shoes for men's comfortable and fashionable versatile black41", "price": 0.0, "currencyCode": "", "currencyName": "", "unit": "", "productNumber": 1.0, "orderType": 0, "countryOfOrigin": "" } ], "gangAoTaiExtVo": null, "swFlag": 0, "sendStatus": 1, "sendStatusName": "Get Successful", "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. Implementation steps
3.1 Creating Java Classes
We will create a name calledMailNoExtractor
Java class used to parse JSON data and extractmailNo
Field.
import ; import ; import ; import ; import ; public class MailNoExtractor { public static List<String> extractMailNos(String json) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = (json); JsonNode recordsNode = ("result").path("records"); List<String> mailNos = new ArrayList<>(); for (JsonNode record : recordsNode) { String mailNo = ("mailNo").asText(); (mailNo); } return mailNos; } public static void main(String[] args) { String json = "{\"msg\":\"The operation was successful!",\"code\":\"ok\",\"result\":{\"records\":[{\"merchantId\":0,\"organId\":0,\"creatorId\":0,\"creatorTruename\":\"\",\"creatorUsername\":\"\",\"organName\":\"\",\"merchantTruename\":\"\",\"merchantUsername\":\"\",\"consigneeName\":\"Sudden Self",\"consigneePhone\":\"153****7763\",\"consigneeTelPhone\":\"*******7763\",\"consigneeCompany\":\"\",\"consigneeProvinceId\":0,\"consigneeProvince\":\"Sichuan Province",\"consigneeCityId\":0,\"consigneeCity\":\"Mianyang City",\"consigneeDistrictId\":0,\"consigneeDistrict\":\"Fucheng District",\"consigneeAddress\":\"Sichuan Province**** Beautiful Beauty\",\"sendProvinceId\":0,\"sendProvince\":\"Guizhou Province",\"sendCityId\":0,\"sendCity\":\"Zunyi City",\"sendDistrictId\":0,\"sendDistrict\":\"Honghuagang District",\"senderName\":\"Yelang Wine Industry\",\"senderCompany\":\"\",\"senderPhone\":\"155****7773\",\"senderTelPhone\":\"*******7773\",\"senderDetailAddress\":\"Guizhou Province **** Transfer Warehouse\",\"mailNo\":\"SF3150804745040\",\"subWaybillNoList\":[],\"waybillStatus\":50,\"printSerialNumber\":\"\",\"printStatus\":0,\"printNumber\":0,\"orderValueAddedVo\":null,\"addedServiceCodes\":[],\"callFlag\":\"\",\"consignment\":\"Yarang Jiang Hongtu Pure Grain Daqu Kunsha Liquor Sauce Flavored Liquor Gift Box 53 Degrees 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\":\"Your express delivery has been signed by me. If you have any questions, please call the courier [Yang Fabin, phone: 13330891935]. Your experience is crucial to us. If you have any ideas and suggestions for our services, please feel free to contact us. We will listen carefully and make every effort to improve and live up to your trust and support.\",\"latestOpCode\":\"80\",\"expressProductId\":0,\"expressProductCode\":\"B1\",\"expressProductName\":\"E-trade mark fast",\"expressPayMethod\":1,\"expressPayMethodName\":\"Send monthly payment\",\"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\":\"Yarang Jiang Hongtu Pure Grain Daqu Kunsha Liquor Sauce Flavored Liquor Gift Box 53 Degrees 500ml\",\"price\":0.0,\"currencyCode\":\"\",\"currencyName\":\"\",\"unit\":\"\",\"productNumber\":2.0,\"orderType\":0,\"countryOfOrigin\":\"\"}],\"gangAoTaiExtVo\":null,\"swFlag\":0,\"sendStatus\":1,\"sendStatusName\":\"Get Successful",\"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\":\"Guizhou Province",\"consigneeCityId\":0,\"consigneeCity\":\"Guiyang City",\"consigneeDistrictId\":0,\"consigneeDistrict\":\"Xuwen County",\"consigneeAddress\":\"Guizhou Province **** Beast Bald Shoe Store\",\"sendProvinceId\":0,\"sendProvince\":\"Guizhou Province",\"sendCityId\":0,\"sendCity\":\"Zunyi City",\"sendDistrictId\":0,\"sendDistrict\":\"Honghuagang District",\"senderName\":\"packet\",\"senderCompany\":\"\",\"senderPhone\":\"155****1949\",\"senderTelPhone\":\"*******1949\",\"senderDetailAddress\":\"Guizhou Province **** Cainiao Station\",\"mailNo\":\"SF3150809345059\",\"subWaybillNoList\":[],\"waybillStatus\":50,\"printSerialNumber\":\"\",\"printStatus\":0,\"printNumber\":0,\"orderValueAddedVo\":null,\"addedServiceCodes\":[],\"callFlag\":\"\",\"consignment\":\"2024 men's shoes for men's comfortable and fashionable versatile black 41\",\"consignmentNumber\":1,\"parcelWeight\":\"2.5\",\"parcelWeightUnit\":\"2.5\",\"parcelQuantity\":1,\"meterageWeightQty\":\"2.5\",\"realWeightQty\":\"2.5\",\"orderNo\":\"6938535195456968508|6780cc24dd560100\",\"originalOrderNo\":\"\",\"signBackNo\":\"\",\"remark\":\"Donkey Orphan 42 digits\",\"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\":\"Your express delivery has been signed by me. If you have any questions, please call the courier [Wang Zhaofa, phone: 18143512989]. Your experience is crucial to us. If you have any ideas and suggestions for our services, please feel free to contact us. We will listen carefully and make every effort to improve and live up to your trust and support.\",\"latestOpCode\":\"80\",\"expressProductId\":0,\"expressProductCode\":\"B1\",\"expressProductName\":\"E-trade mark fast",\"expressPayMethod\":1,\"expressPayMethodName\":\"Send monthly payment\",\"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 men's shoes for men's comfortable and fashionable versatile black 41\",\"price\":0.0,\"currencyCode\":\"\",\"currencyName\":\"\",\"unit\":\"\",\"productNumber\":1.0,\"orderType\":0,\"countryOfOrigin\":\"\"}],\"gangAoTaiExtVo\":null,\"swFlag\":0,\"sendStatus\":1,\"sendStatusName\":\"Get Successful",\"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) { (mailNo); } } catch (IOException e) { (); } } }
3.2 Code parsing
3.2.1 extractMailNos method
-
Function: This method takes a JSON string as input, parses JSON using the Jackson library, and extracts all
mailNo
The value of the field returns a containing allmailNo
list. -
step:
- use
ObjectMapper
Parsing JSON strings toJsonNode
Object. - pass
path
Method Gettingresult
Under the noderecords
Array. - Traversal
records
array, extract themailNo
field and add it to the list.
- use
3.2.2 main method
-
Function: This is a simple test method that shows how to use it
extractMailNos
Method to extractmailNo
And print it out. -
step:
- Define a JSON string.
- Call
extractMailNos
Method ExtractionmailNo
。 - Iterate over and print all
mailNo
。
3.3 Operation results
After running the above code, the output result is as follows:
SF3150804745040
SF3150809345059
4. Summary
This article details how to parse JSON data and extract specific fields using Java. By using the Jackson library, we can easily process complex JSON data structures and extract the fields we need. This tool class can be further expanded and optimized according to actual needs, such as adding exception handling, supporting more fields extraction, etc.
I hope this article will be helpful for you to understand and use Java to process JSON data!
This is the article about the implementation steps of using Java to parse JSON data and extract specific fields (taking the extraction of mailNo as an example). For more related Java to parse JSON data and extract field content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!