jsonpath的使用(java:List<Object>转List<HashMap>)
要求及被测试字符串
练习1:从下面提取token
{"code":9420, "msg":"恭喜qzcsbj,登录成功","token":"538bbaba44be5d3d3856718e6c637d02"}
格式化
{ "code": 9420, "msg": "恭喜qzcsbj,登录成功", "token": "538bbaba44be5d3d3856718e6c637d02" }
练习2:从下面提取username是“韧”的phone
{"code":"0","msg":"sucess","data":[{"username":"韧","realname":"tester1","sex":"1","phone":"13800000001"},{"username":"全栈测试笔记","realname":"tester2","sex":"1","phone":"13800000002"}]}
格式化
{ "code": "0", "msg": "sucess", "data": [ { "username": "韧", "realname": "tester1", "sex": "1", "phone": "13800000001" }, { "username": "全栈测试笔记", "realname": "tester2", "sex": "1", "phone": "13800000002" } ] }
依赖
<!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path --> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <version>2.4.0</version> </dependency>
实现
获取关联值:token
获取断言字段值:code
package com.qzcsbj; import com.alibaba.fastjson.JSONObject; import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; import java.util.HashMap; import java.util.List; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class Test { public static void main(String[] args) { String res = "{\"code\":9420, \"msg\":\"恭喜qzcsbj,登录成功\",\"token\":\"538bbaba44be5d3d3856718e6c637d02\"}"; Object document = Configuration.defaultConfiguration().jsonProvider().parse(res); String token = JsonPath.read(document, "$.token"); System.out.println("token: " + token); String res2 = "{\"code\":\"0\",\"msg\":\"sucess\",\"data\":[{\"username\":\"韧\",\"realname\":\"tester1\",\"sex\":\"1\",\"phone\":\"13800000001\"},{\"username\":\"全栈测试笔记\",\"realname\":\"tester2\",\"sex\":\"1\",\"phone\":\"13800000002\"}]}"; Object document2 = Configuration.defaultConfiguration().jsonProvider().parse(res2); String phone = JsonPath.read(document2, "$.data[0].phone"); System.out.println("phone: " + phone); List<Object> arr = JsonPath.read(document2, "$.data[*].phone"); // jsonpath不能写成:$.data[*].phone[?(@.username=='韧')] System.out.println("arr: " + arr.toString()); List<Object> arr2 = JsonPath.read(document2, "$.data[*].['username','phone']"); boolean flag = true; System.out.println("arr2: " + arr2.toString()); List<HashMap> maps = JSONObject.parseArray(JSONObject.toJSONString(arr2),HashMap.class); // 第一个参数也可以写为:arr2.toString();第二个参数是字节码,说明源码用到了反射 for (HashMap map : maps) { if ("韧".equals(map.get("username"))){ System.out.println(map.get("username")+"的phone是:" + map.get("phone")); flag = false; break; } } if (flag){ System.out.println("未获取到【韧】的phone"); } } }
补充:
List<User> users = JSONObject.parseArray(parameters, User.class);
参考:https://www.cnblogs.com/uncleyong/p/16683578.html
原文会持续更新,原文地址:https://www.cnblogs.com/uncleyong/p/16676791.html
__EOF__
本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!