zoukankan      html  css  js  c++  java
  • Java测试开发--JSONPath、JSONArray、JSONObject使用(十)

    一、Maven项目,pom.xml文件中导入

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.75</version>
            </dependency>

    二、JSONPath 使用时,注意绝对路径和相对路径,实例如下

    { "store": {
        "book": [ 
          { "category": "reference",
            "author": "Nigel Rees",
            "title": "Sayings of the Century",
            "price": 8.95
          },
          { "category": "fiction",
            "author": "Evelyn Waugh",
            "title": "Sword of Honour",
            "price": 12.99,
            "isbn": "0-553-21311-3"
          }
        ],
        "bicycle": {
          "color": "red",
          "price": 19.95
        }
      }
    }

    上面一段json数据,获取价格字段的值

    String  jsonStr = "...."

    绝对路径

    List<Double> prices =  (List<Double>) JSONPath.read(jsonStr, "$.store.book.price");

    相对路径

    List<String> names =  (List<String>) JSONPath.read(jsonStr, "..price");

    三、JSON转map

    String json="{"msg":"登录成功","uid":"9CC972DFA2D4481F89841A46FD1B3E7B","code":"1"}";

    Map map = JSON.parseObject(json,Map.class);
    System.out.println("map==="+map.get("uid"));

  • 相关阅读:
    未解决的
    nodejs 7 和 8 的比较
    openresty Nginx
    Vim快捷键分类
    wireshark 包过滤
    RSA 公私钥 互换问题
    vim命令
    Windows 小端存储
    python 字符转换
    ssl证书验证
  • 原文地址:https://www.cnblogs.com/cyying/p/15194608.html
Copyright © 2011-2022 走看看