当前位置: 代码网 > it编程>编程语言>Java > 使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)

使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)

2025年01月13日 Java 我要评论
1. 背景介绍1.1 json简介json(javascript object notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。它基于javascript的一

1. 背景介绍

1.1 json简介

json(javascript object notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。它基于javascript的一个子集,但独立于语言,几乎所有的编程语言都支持json格式的数据处理。

1.2 实际案例

假设我们有一个json数据,其中包含多个订单信息,每个订单都有一个唯一的mailno字段。我们的目标是从这个json数据中提取出所有的mailno字段,并将其存储在一个列表中。

2. 准备工作

2.1 环境搭建

为了解析json数据,我们需要使用一个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数据结构

以下是我们将要处理的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": "绵阳市",
        "consigneedistrictid": 0,
        "consigneedistrict": "涪城区",
        "consigneeaddress": "四川省****靓妍美妆",
        "sendprovinceid": 0,
        "sendprovince": "贵州省",
        "sendcityid": 0,
        "sendcity": "遵义市",
        "senddistrictid": 0,
        "senddistrict": "红花岗区",
        "sendername": "夜郎酒业",
        "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": "您的快件已由本人签收,如有疑问请电联快递员【杨发斌,电话:13330891935】。您的体验对于我们至关重要,如果您对我们的服务有任何的想法和建议,请随时联系我们,我们一定用心倾听,全力改进,不辜负您的信任与支持。",
        "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": "红花岗区",
        "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": "您的快件已由本人签收,如有疑问请电联快递员【王昭发,电话:18143512989】。您的体验对于我们至关重要,如果您对我们的服务有任何的想法和建议,请随时联系我们,我们一定用心倾听,全力改进,不辜负您的信任与支持。",
        "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. 实现步骤

3.1 创建java类

我们将创建一个名为mailnoextractor的java类,用于解析json数据并提取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\":\"绵阳市\",\"consigneedistrictid\":0,\"consigneedistrict\":\"涪城区\",\"consigneeaddress\":\"四川省****靓妍美妆\",\"sendprovinceid\":0,\"sendprovince\":\"贵州省\",\"sendcityid\":0,\"sendcity\":\"遵义市\",\"senddistrictid\":0,\"senddistrict\":\"红花岗区\",\"sendername\":\"夜郎酒业\",\"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\":\"您的快件已由本人签收,如有疑问请电联快递员【杨发斌,电话:13330891935】。您的体验对于我们至关重要,如果您对我们的服务有任何的想法和建议,请随时联系我们,我们一定用心倾听,全力改进,不辜负您的信任与支持。\",\"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\":\"红花岗区\",\"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\":\"您的快件已由本人签收,如有疑问请电联快递员【王昭发,电话:18143512989】。您的体验对于我们至关重要,如果您对我们的服务有任何的想法和建议,请随时联系我们,我们一定用心倾听,全力改进,不辜负您的信任与支持。\",\"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的列表。
  • 步骤
    1. 使用objectmapper将json字符串解析为jsonnode对象。
    2. 通过path方法获取result节点下的records数组。
    3. 遍历records数组,提取每个记录中的mailno字段,并将其添加到列表中。

3.2.2 main 方法

  • 功能:这是一个简单的测试方法,展示了如何使用extractmailnos方法来提取mailno并打印出来。
  • 步骤
    1. 定义一个json字符串。
    2. 调用extractmailnos方法提取mailno
    3. 遍历并打印所有mailno

3.3 运行结果

运行上述代码后,输出结果如下:

sf3150804745040
sf3150809345059

4. 总结

本文详细介绍了如何使用java解析json数据并提取特定字段。通过使用jackson库,我们可以轻松地处理复杂的json数据结构,并提取出我们需要的字段。这个工具类可以根据实际需求进一步扩展和优化,例如添加异常处理、支持更多字段的提取等。

希望本文对你理解和使用java处理json数据有所帮助!

到此这篇关于使用java解析json数据并提取特定字段的实现步骤(以提取mailno为例)的文章就介绍到这了,更多相关java解析json数据并提取字段内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com