zoukankan      html  css  js  c++  java
  • Java如何获取JSON数据中的值

    场景:在接口自动化场景中,下个接口发送的请求参数,依赖上个接口请求结果中的值。需要将获取值作为全局参数引用。

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import com.alibaba.fastjson.JSONObject;
    
    public class getJSONValue {
    
        public static void main(String[] args) {
            String charset = "utf-8";
            File file = new File("D:\XX\JSON.txt");
            long fileByteLength = file.length();
            byte[] content = new byte[(int) fileByteLength];
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(file);
                fileInputStream.read(content);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String str = null;
            try {
                str = new String(content, charset);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            JSONObject object = (JSONObject) JSONObject.parse(str);
            System.out.println(object.getJSONObject("testsetTestcaseExecute").get("auditor"));
            System.out.println(object.getJSONObject("testsetTestcaseExecute").get("testcaseType"));
        }
    }
    {"testsetTestcaseExecute":{"auditor":"vame","testcaseType":"Exception"}}
    
    vame
    Exception
  • 相关阅读:
    玲珑学院-ACM比赛1014
    扩展欧几里得算法
    中国剩余定理(孙子定理)及实现----原理详解
    搞懂树状数组
    HDU3792---Twin Prime Conjecture(树状数组)
    树状数组 模板
    HDU1541--Stars(树状数组)
    HDU4046--Panda(树状数组)
    CCF-201604-1-折点计数
    CCF-201604-2-俄罗斯方块
  • 原文地址:https://www.cnblogs.com/Shanghai-vame/p/10009333.html
Copyright © 2011-2022 走看看