Java从json串中获取具体的某个的数值-创新互联

在做百度VCR发票识别中遇到了如下的问题,百度接口返回给我了json格式的字符串,那么毫无疑问我需要进行处理一下。

成都创新互联于2013年创立,先为峰峰矿等服务建站,峰峰矿等地企业,进行企业商务咨询服务。为峰峰矿企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

返回回来的代码如下:

{
    "words_result_num": 45,
    "words_result": {
        "CommodityUnit": [
            {
                "row": "1",
                "word": "件"
            }
        ],
        "PurchaserAddress": "",
        "SheetNum": "",
        "CommodityType": [
            {
                "row": "1",
                "word": "DHE-8011耳机"
            }
        ],
        "TotalAmount": "34.51",
        "Checker": "王燕",
        "PurchaserBank": "",
        "Agent": "否",
        "Password": "645<++0+253*899/40>93067755>19/<04025693>*52/83-00-*<<3-*+3181581>>>6824*1>8/63067755>19/<04023>37",
        "InvoiceTypeOrg": "江苏增值税电子普通发票",
        "InvoiceCodeConfirm": "032002100811",
        "TotalTax": "4.49",
        "ServiceType": "电器设备",
        "CommodityTaxRate": [
            {
                "row": "1",
                "word": "13%"
            }
        ],
        "CommodityTax": [
            {
                "row": "1",
                "word": "4.49"
            }
        ],
        "SellerBank": "中国建设银行南京徐庄软件园支行32001881436052508322",
        "Remarks": "38214257941",
        "SellerAddress": "南京市玄武区玄武大道699-19号8幢025-66996699",
        "NoteDrawer": "刘娅",
        "InvoiceTag": "其他",
        "InvoiceNumConfirm": "58067464",
        "OnlinePay": "",
        "Payee": "施怡红",
        "CommodityName": [
            {
                "row": "1",
                "word": "*计算机配套产品*hp/惠普DHE-8011耳机"
            }
        ],
        "CommodityVehicleType": [],
        "InvoiceCode": "032002100811",
        "AmountInWords": "叁拾玖圆整",
        "AmountInFiguers": "39.00",
        "City": "",
        "InvoiceType": "电子普通发票",
        "CommodityEndDate": [],
        "PurchaserName": "个人",
        "InvoiceDate": "2022年06月20日",
        "CommodityNum": [
            {
                "row": "1",
                "word": "1"
            }
        ],
        "PurchaserRegisterNum": "",
        "MachineCode": "661616292450",
        "CommodityPlateNum": [],
        "CheckCode": "77221101492528330127",
        "SellerRegisterNum": "91320102575938948G",
        "CommodityPrice": [
            {
                "row": "1",
                "word": "34.51327434"
            }
        ],
        "CommodityStartDate": [],
        "SellerName": "南京苏宁易购电子商务有限公司",
        "CommodityAmount": [
            {
                "row": "1",
                "word": "34.51"
            }
        ],
        "Province": "江苏省",
        "InvoiceNum": "58067464"
    },
    "log_id": 1598565862431452614
}

打印台出的为:

这边我写了一个方法进行处理,这里举出一个典型的例子以获取CommodityUnit的row和获取InvoiceDate时间为例

public static String getHashMapByJson(String jsonResult) {
        JSONObject jsonObject = new JSONObject(jsonResult);
        //将json格式转化为指定的map集合
        Map>>>map=(Map)jsonObject.toMap();
        String words_result = map.get("words_result").toString();
        String s = map.get("words_result").get("CommodityUnit").get(0).get("row").toString();
        Map>map1=(Map)jsonObject.toMap();
        //获取发票日期
        String invTime = map1.get("words_result").get("InvoiceDate");
        return invTime;
    }

最后在测试段成功获取

测试端代码如下,

测试的时间调用方法调取方法

String invTime  = BaiDuOCR.getHashMapByJson(jsonResult);
            System.out.println(invTime);

打印内容如下:

测试CommodityUnit的row调用的方法

String s  = BaiDuOCR.getHashMapByJson(jsonResult);
            System.out.println(s);

运行成功

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


新闻名称:Java从json串中获取具体的某个的数值-创新互联
本文来源:http://scyanting.com/article/hjeip.html