下载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());
}