zoukankan      html  css  js  c++  java
  • 接口测试代码中,json字符串、对象的常用转换

    做接口测试时,有时会需要转换接口返回值并提取其中的数据,以进行断言,本文基于使用阿里的fastjson包的前提下,整理了一些最常用的类型转换:

    1.将json字符串 → List集合

    String json = “{'name':zhangsan,'age':34,'sex':female,'department’:qa}";
    String[] arr = json.substring(1,json.length()-1).split(",");
    List<String> list = Arrays.asList(arr);
    for(String str:list) {
      System.out.println(str);
    }

    2.json字符串 → java数组

    String str1 = "{"name":"袁野","age":18,"career":"student"}";
    
    //json字符串转换成java数组
    String[] strArr = str1.substring(1,str1.length()-1).split(",");

    3.json字符串 → java对象

    String str1 = "{"id":1,"username":"袁野","password":"123456","career":"student"}";
    User user = JSONObject.parseObject(str1,User.class);

    4.json字符串 → jJSONObject

    public void test(){
      String str = "{"name":"袁野","age":18,"career":"student"}";
      JSONObject jsonObject = JSONObject.parseObject(str);
    }

    5.json字符串 → JSONArray

    String jsonStr1 = "[{"id":1,"name":"王立波","age":28,"password":"123456"}]";
    JSONArray array = JSONArray.parseArray(jsonStr1);

    6.java对象 → json字符串,再转为JSONObject、JSONArray

    public void test(){
      User user = new User();
      user.setId(1);
      user.setUsername("西施");
      user.setPassword("123456");
      user.setEmail("xishi@skio.cn");
      user.setCreate_time("2020-01-02");
    
      String str2 = JSONObject.toJSONString(user);
        //底层是用Map实现的,也可以用Map来接收
      JSONObject jsonObject = JSONObject.parseObject(str2);
      JSONArray jsonArray = JSONArray.parseArray("["+str2+"]");
    }

    7.java对象列表 → json对象数组

    List<User> userList = new ArrayList<>();
    userList.add(new User());
    userList.add(new User());
    String str = JSONObject.toJSONString(userList);
    JSONArray jsonArray = JSONArray.parseArray(str);

    8.在JOSNObject中,提取JSONArray

    JSONObject jsonObject = JSONObject.parseObject(str1);
    JSONArray jsonArray = jsonObject.getJSONArray("data");

    9.JSONArray中,提取JSONObject及遍历

    public void test(){
        String str1 = "[{"id":1,"username":"袁野","password":"123456","career":"student"}," +
                "{"id":2,"username":"方文斌","password":"123456","career":"teacher"}," +
                "{"id":3,"username":"黎明","password":"123456","career":"teacher"}]";
    //    String[] arr = str1.substring(1,str1.length()-1).split(",");
    
        JSONArray jsonArray = JSONArray.parseArray(str1);
        JSONObject jsonObject = jsonArray.getJSONObject(0);
        for(Object obj:jsonArray) {
          JSONObject o = JSONObject.parseObject(obj.toString());
        }
      }

    10. JSON字符串 → java对象列表

    public void test(){
        String str1 = "[{"id":1,"username":"袁野","password":"123456","email":"test1@skio.cn","create_time":"2020-02-15"}," +
                "{"id":2,"username":"方辉","password":"123456","email":"test2@skio.cn","create_time":"2020-02-16"}," +
                "{"id":3,"username":"沈斌","password":"123456","email":"test3@skio.cn","create_time":"2020-02-17"}]";
    
        List<User> userList = new ArrayList<>();
        JSONArray jsonArray = JSONArray.parseArray(str1);
        User user;
        for(Object obj:jsonArray) {
          user = JSONObject.parseObject(JSONObject.toJSONString(obj),User.class);
          userList.add(user);
        }
    }
  • 相关阅读:
    跳出iframe
    leetcode 225. Implement Stack using Queues
    leetcode 206. Reverse Linked List
    leetcode 205. Isomorphic Strings
    leetcode 203. Remove Linked List Elements
    leetcode 198. House Robber
    leetcode 190. Reverse Bits
    leetcode leetcode 783. Minimum Distance Between BST Nodes
    leetcode 202. Happy Number
    leetcode 389. Find the Difference
  • 原文地址:https://www.cnblogs.com/May-study/p/12713845.html
Copyright © 2011-2022 走看看