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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM