fastjson.JSONPath使用舉例


fastjson.JSONPath使用舉例

源碼:

/**
         * 根據路徑獲取值
         * */
        String bjnr = (String) JSONPath.read(json, "$.data.bjnr");
        System.out.println("報警內容:" + bjnr);

        String isInvolved = (String) JSONPath.read(json, "$.data.isInvolved");
        System.out.println("報警人是否是涉案人:" + isInvolved);

        /**
         * 獲取JSON中的對象數組
         * */
        List<JSONObject> hwList = (List<JSONObject>) JSONPath.read(json, "$.data.hwList");
        System.out.println("hwList:" + hwList);

        /**
         * 獲取JSON中的所有id的值
         * */
        List<String> ids = (List<String>) JSONPath.read(json, "$..id");
        System.out.println("ids:" + ids);

        /**
         * 可以提前編輯一個路徑,並多次使用它
         * */
        JSONPath path = JSONPath.compile("$.data.keywords");
        System.out.println("keywords:" + path.eval(JSON.parseObject(json)));

 

運行結果:

 

報警內容:報警人稱發現一名小偷(男性,30歲左右,1米63,棕色外套,紅色毛衣)往好利達超市方向逃跑。
報警人是否是涉案人:2
hwList:[{"count":46266,"section":"10000以上","id":24,"highFWords":"報警"},{"count":17806,"section":"10000以上","id":26,"highFWords":"人稱"}]
ids:[24, 26]
keywords:人稱發現,發現小偷,方向逃跑

 

測試Json:

{
    "code": 200,
    "message": "查詢成功",
    "data": {
        "jjdbh": "3323422",
        "danger": "",
        "keywords": "人稱發現,發現小偷,方向逃跑",
        "verb": "發現,逃跑",
        "noun": "報警,人稱,小偷,男性,棕色,外套,毛衣,利達,超市,方向",
        "adv": "往好",
        "bjnr": "報警人稱發現一名小偷(男性,30歲左右,1米63,棕色外套,紅色毛衣)往好利達超市方向逃跑。",
        "fxwp": "2",
        "hwList": [{
            "id": 24,
            "highFWords": "報警",
            "count": 46266,
            "pos": null,
            "section": "10000以上"
        }, {
            "id": 26,
            "highFWords": "人稱",
            "count": 17806,
            "pos": null,
            "section": "10000以上"
        }],
        "isInvolved": "2"
    }
}

 

end

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM