遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

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

 

posted @ 2022-09-10 16:23  全栈测试笔记  阅读(872)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end