下載org.json包(文檔說明)
#下載地址
https://www.json.org/
# github 地址
https://github.com/stleary/JSON-java
# api 文檔說明
https://resources.arcgis.com/en/help/arcobjects-java/api/arcobjects/com/esri/arcgis/server/json/JSONObject.html
BeanShell腳本
import org.json.JSONObject;
try {//接受登錄返回json字符串進行處理String jsonResponse = prev.getResponseDataAsString();//log.info("Response: " + jsonResponse);//去除外層雙引號if (jsonResponse.startsWith("\"") && jsonResponse.endsWith("\"")) {jsonResponse = jsonResponse.substring(1, jsonResponse.length() - 1);}//轉義處理jsonResponse = jsonResponse.replace("\\\"", "\"");//清理隱藏字符jsonResponse = jsonResponse.replaceAll("^\uFEFF", "").trim();byte[] resultByt = jsonResponse.getBytes();//回寫設置結果prev.setResponseData(resultByt);// log.info("Response_end: " + jsonResponse);JSONObject json = new JSONObject(jsonResponse);// log.info("json: " + json);String token = json.getString("Token");log.info("auth_token: " + token);// 設置gToken變量vars.put("auth_token", token);//log.info("獲取auth_token: " + vars.get("auth_token"));// 作為全局變量props.put("g_auth_token", token);//log.info("獲取g_auth_token: " + props.get("g_auth_token"));} catch (Exception e) {//解析錯誤,輸出錯誤信息log.error("JSON解析錯誤: " + e.getMessage());}
使用腳本斷言
try{//接受登錄返回json字符串進行處理String jsonResponse = prev.getResponseDataAsString();log.info("Response_斷言: " + jsonResponse);//狀態碼斷言log.info("狀態碼:" + ResponseCode);if(ResponseCode.equals("200")){ Failure=false; // 表示斷言成功}else{Failure=true; // 表示斷言失敗FailureMessage="響應狀態碼非200"; // 自定義的失敗信息}} catch (Exception e) {//解析錯誤,輸出錯誤信息log.error("JSON解析錯誤: " + e.getMessage());}