小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

json解析神器 jsonpath的使用

 Levy_X 2017-12-19

如果項(xiàng)目需求是從某些復(fù)雜的json里面取值進(jìn)行計(jì)算,用jsonpath IK(ik-expression)來(lái)處理十分方便,jsonpath用來(lái)取json里面的值然后用IK自帶的函數(shù)進(jìn)行計(jì)算,如果是特殊的計(jì)算那就自定義IK方法搞定,配置化很方便.

下面簡(jiǎn)單介紹下jsonpath的使用方法,主要測(cè)試都在JsonPathDemo類(lèi)里面:

下面是一個(gè)簡(jiǎn)單的java項(xiàng)目demo:

這里寫(xiě)圖片描述

注意: 其中他的max,min,avg,stddev函數(shù)只能類(lèi)似于如下處理:

//正確寫(xiě)法 但是感覺(jué)很雞肋 context.read('$.avg($.result.records[0].loan_type,$.result.records[1].loan_type,$.result.records[2].loan_type)');
  • 1
  • 2

不能傳入list 感覺(jué)比較雞肋,如果傳入list 他會(huì)報(bào)錯(cuò)(如下錯(cuò)誤寫(xiě)法):

//這樣會(huì)報(bào)錯(cuò) Object maxV = context.read('$.max($.result.records[*].loan_type)'); //這樣也會(huì)報(bào)錯(cuò) Object maxV = context.read('$.result.records[*].loan_type.max()'); //如果json文件中是這樣:'loan_type':'2',也會(huì)報(bào)錯(cuò),'loan_type':2 這樣才被認(rèn)為是數(shù)字
  • 1
  • 2
  • 3
  • 4
  • 5

報(bào)錯(cuò)信息都一樣,如下:

Exception in thread 'main' com.jayway.jsonpath.JsonPathException: Aggregation function attempted to calculate value using empty array
  • 1

JsonPathDemo是一個(gè)測(cè)試demo:

public class JsonPathDemo { public static void main(String[] args) { String json = FileUtils.readFileByLines('demo.json'); ReadContext context = JsonPath.parse(json); //1 返回所有name List<String> names = context.read('$.result.records[*].name'); //['張三','李四','王五'] System.out.println(names); //2 返回所有數(shù)組的值 List<Map<String, String>> objs = context.read('$.result.records[*]'); //[{'name':'張三','pid':'500234199212121212','mobile':'18623456789','applied_at':'3','confirmed_at':'5','confirm_type':'overdue','loan_type':'1','test':'mytest','all':'2'},{'name':'李四','pid':'500234199299999999','mobile':'13098765432','applied_at':'1','confirmed_at':'','confirm_type':'overdue','loan_type':'3','all':'3'},{'name':'王五','pid':'50023415464654659','mobile':'1706454894','applied_at':'-1','confirmed_at':'','confirm_type':'overdue','loan_type':'3'}] System.out.println(objs); //3 返回第一個(gè)的name String name0 = context.read('$.result.records[0].name'); //張三 System.out.println(name0); //4 返回下標(biāo)為02 的數(shù)組值 List<String> name0and2 = context.read('$.result.records[0,2].name'); //['張三','王五'] System.out.println(name0and2); //5 返回下標(biāo)為0 到 下標(biāo)為1的 的數(shù)組值 這里[0:2] 表示包含0 但是 不包含2 List<String> name0to2 = context.read('$.result.records[0:2].name'); //['張三','李四'] System.out.println(name0to2); //6 返回?cái)?shù)組的最后兩個(gè)值 List<String> lastTwoName = context.read('$.result.records[-2:].name'); //['李四','王五'] System.out.println(lastTwoName); //7 返回下標(biāo)為1之后的所有數(shù)組值 包含下標(biāo)為1的 List<String> nameFromOne = context.read('$.result.records[1:].name'); //['李四','王五'] System.out.println(nameFromOne); //8 返回下標(biāo)為3之前的所有數(shù)組值 不包含下標(biāo)為3的 List<String> nameEndTwo = context.read('$.result.records[:3].name'); //['張三','李四','王五'] System.out.println(nameEndTwo); //9 返回applied_at大于等于2的值 List<Map<String, String>> records = context.read('$.result.records[?(@.applied_at >= '2')]'); //[{'name':'張三','pid':'500234199212121212','mobile':'18623456789','applied_at':'3','confirmed_at':'5','confirm_type':'overdue','loan_type':'1','test':'mytest','all':'2'}] System.out.println(records); //10 返回name等于李四的值 List<Map<String, String>> records0 = context.read('$.result.records[?(@.name == '李四')]'); //[{'name':'李四','pid':'500234199299999999','mobile':'13098765432','applied_at':'1','confirmed_at':'','confirm_type':'overdue','loan_type':'3'}] System.out.println(records0); //11 返回有test屬性的數(shù)組 List<Map<String, String>> records1 = context.read('$.result.records[?(@.test)]'); //[{'name':'張三','pid':'500234199212121212','mobile':'18623456789','applied_at':'3','confirmed_at':'5','confirm_type':'overdue','loan_type':'1','test':'mytest','all':'2'}] System.out.println(records1); //12 返回有test屬性的數(shù)組 List<String> list = context.read('$..all'); //['1','4','2','3'] System.out.println(list); //12 以當(dāng)前json的某個(gè)值為條件查詢(xún) 這里ok為1 取出records數(shù)組中applied_at等于1的數(shù)組 List<String> ok = context.read('$.result.records[?(@.applied_at == $['ok'])]'); //['1','4','2','3'] System.out.println(ok); //13 正則匹配 List<String> regexName = context.read('$.result.records[?(@.pid =~ /.*999/i)]'); //[{'name':'李四','pid':'500234199299999999','mobile':'13098765432','applied_at':'1','confirmed_at':'','confirm_type':'overdue','loan_type':'3','all':'3'}] System.out.println(regexName); //14 多條件 List<String> mobile = context.read('$.result.records[?(@.all == '2' || @.name == '李四' )].mobile'); //['18623456789','13098765432'] System.out.println(mobile); //14 查詢(xún)數(shù)組長(zhǎng)度 Integer length01 = context.read('$.result.records.length()'); //3 System.out.println(length01); //15 查詢(xún)list里面每個(gè)對(duì)象長(zhǎng)度 List<Integer> length02 = context.read('$.result.records[?(@.all == '2' || @.name == '李四' )].length()'); //[9,8] System.out.println(length02); //16 最大值 Object maxV = context.read('$.max($.result.records[0].loan_type,$.result.records[1].loan_type,$.result.records[2].loan_type)'); //3.0 System.out.println(maxV); //17 最小值 Object minV = context.read('$.min($.result.records[0].loan_type,$.result.records[1].loan_type,$.result.records[2].loan_type)'); //1.0 System.out.println(minV); //18 平均值 double avgV = context.read('$.avg($.result.records[0].loan_type,$.result.records[1].loan_type,$.result.records[2].loan_type)'); //2.3333333333333335 System.out.println(avgV); //19 標(biāo)準(zhǔn)差 double stddevV = context.read('$.stddev($.result.records[0].loan_type,$.result.records[1].loan_type,$.result.records[2].loan_type)'); //0.9428090415820636 System.out.println(stddevV); //20 讀取一個(gè)不存在的 String haha = context.read('$.result.haha'); //拋出異常 //Exception in thread 'main' com.jayway.jsonpath.PathNotFoundException: No results for path: $['result']['haha'] //at com.jayway.jsonpath.internal.path.EvaluationContextImpl.getValue(EvaluationContextImpl.java:133) //at com.jayway.jsonpath.JsonPath.read(JsonPath.java:187) //at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:102) //at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:89) //at cn.lijie.jsonpath.JsonPathDemo.main(JsonPathDemo.java:58) //at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) //at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) //at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) //at java.lang.reflect.Method.invoke(Method.java:498) //at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) System.out.println(haha); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127

pom文件引入:

<dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <version>2.3.0</version> </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

其中demo.json是一個(gè)測(cè)試json:

{ 'action': '/interface.service/xxx/queryBlackUserData', 'all': '1', 'result': { 'count': 2, 'tenant_count': 2, 'records': [ { 'name': '張三', 'pid': '500234199212121212', 'mobile': '18623456789', 'applied_at': '3', 'confirmed_at': '5', 'confirm_type': 'overdue', 'loan_type': 1, 'test': 'mytest', 'all': '2' }, { 'name': '李四', 'pid': '500234199299999999', 'mobile': '13098765432', 'applied_at': '1', 'confirmed_at': '', 'confirm_type': 'overdue', 'loan_type': 3, 'all': '3' }, { 'name': '王五', 'pid': '50023415464654659', 'mobile': '1706454894', 'applied_at': '-1', 'confirmed_at': '', 'confirm_type': 'overdue', 'loan_type': 3 } ], 'all': '4' }, 'code': 200, 'subtime': '1480495123550', 'status': 'success', 'ok': 3 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

FileUtils類(lèi)是用于讀取xx.json文件為字符串的json:

public class FileUtils { /** * 以行為單位讀取文件,常用于讀面向行的格式化文件 */ public static String readFileByLines(String fileName) { File file = new File(fileName); BufferedReader reader = null; String str = ''; try { InputStream is = FileUtils.class.getClassLoader().getResourceAsStream(fileName); reader = new BufferedReader(new InputStreamReader(is)); String tempString = null; int line = 1; // 一次讀入一行,直到讀入null為文件結(jié)束 while ((tempString = reader.readLine()) != null) { // 顯示行號(hào) str = tempString; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return str; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多