性能测试工具 Jmeter-Json 串中含转义字符,使用 JSON Extractor 提取失败

猫大白 · 2019年01月02日 · 最后由 hello舒乡长 回复于 2019年08月27日 · 5161 次阅读

响应如下:

{"code":0,"msg":"SUCCESS","data":[{"deviceid":"29083","inverterserial":"","commcardserial":"","pcsmodel":"Mario 6.0 ","removed":0,"modified":"2018-07-04T09:08:30.161Z","created":"2018-07-04T09:08:30.161Z","config":"{\"deviceID\":\"29083\",\"cmd_id\":\"Cov7Us2WVe\",\"timestamp\":\"1545639511\",\"inverterSerial\":\" \",\"commcardSerial\":\"\",\"commcardVersion\":\"1.0.0\",\"network\":{\"type\":\"ethernet\",\"macAddress\":\"00:30:AB:FF8:0E\",\"ipAddress\":\"\"},\"swLastUpdatedTime\":\"1542008395\",\"pcsModel\":\"Mario 6.0 \",\"inverterHwVer\":\"00.02\",\"inverterSwVer\":\"\",\"time\":{\"timeZone\":\"8\",\"utcTimeCmd\":\"1545369829\"},\"batteryInfo\":[{\"batteryModel\":\"\",\"batterySerial\":\"0\",\"batteryCapacity\":\"9800\"},{\"batteryModel\":\"\",\"batterySerial\":\"0\",\"batteryCapacity\":\"0\"}],\"pcsOutLimit\":\"6000\",\"pcsOpMode\":\"0\",\"TOU\":{\"chargeStart\":\"19\",\"chargeEnd\":\"30\",\"chargePower\":\"40\",\"dischargeStart\":\"50\",\"dischargeEnd\":\"60\",\"dischargePower\":\"300\"},\"backupSOC\":\"30\",\"gridSelector\":{\"gridType\":\"17\",\"utility\":\"3\"},\"gridStatus\":\"0\",\"rule21\":{\"synchronization\":{\"syncTime\":\"500.00\",\"vLow\":\"89.0\",\"vHigh\":\"105.0\",\"freqLow\":\"59.66\",\"freqHigh\":\"61.00\"},\"ramp\":{\"softStartRamp\":\"888.0\",\"normalRamp\":\"888.0\"},\"lhvrt\":{\"ov2Voltage\":\"120\",\"ov2Time\":\"0.16\",\"ov1Voltabe\":\"115\",\"ov1Time\":\"12.00\",\"uv1Voltage\":\"88\",\"uv1Time\":\"50.00\",\"uv2Voltage\":\"80\",\"uv2Time\":\"20.00\",\"uv3Voltage\":\"50\",\"uv3Time\":\"0.16\"},\"lhfrt\":{\"of2Freq\":\"64.50\",\"of2Time\":\"2.00\",\"of1Freq\":\"63.00\",\"of1Time\":\"21.00\",\"uf1Freq\":\"58.88\",\"uf1Time\":\"21.00\",\"uf2Freq\":\"56.00\",\"uf2Time\":\"22.00\"},\"powerPriority\":\"0\",\"activePowerControl\":{\"mode\":\"1\",\"fixP\":{\"maxPower\":\"99.0\"},\"v1\":\"105.0\",\"p1\":\"100.0\",\"v2\":\"109.0\",\"p2\":\"0.0\",\"voltWatt\":{\"timeConst\":\"100\"},\"freqWatt\":{\"hyst\":\"1\",\"startFreq\":\"2.00\",\"endFreq\":\"3.00\",\"gradient\":\"600.0\",\"timeConst\":\"80\"}},\"reactivePowerControl\":{\"mode\":\"1\",\"fixPf\":{\"cosPhi\":\"-0.950\"},\"voltVar\":{\"v1\":\"110.0\",\"q1\":\"100.0\",\"v2\":\"110.0\",\"q2\":\"100.0\",\"v3\":\"110.0\",\"q3\":\"100.0\",\"v4\":\"100.0\",\"q4\":\"-44.0\",\"timeConst\":\"100\"}}}}","online":1}]}

使用 JSON Extractor 提取失败:$.data[0].config.cmd_id

共收到 7 条回复 时间 点赞

可以试试正则提取;

对转义的再此转义

{
"code": 0,
"msg": "SUCCESS",
"data": [
{
"deviceid": "29083",
"inverterserial": "",
"commcardserial": "",
"pcsmodel": "Mario 6.0 ",
"removed": 0,
"modified": "2018-07-04T09:08:30.161Z",
"created": "2018-07-04T09:08:30.161Z",
"config": "{\"deviceID\":\"29083\",\"cmd_id\":\"Cov7Us2WVe\",\"timestamp\":\"1545639511\",\"inverterSerial\":\" \",\"commcardSerial\":\"\",\"commcardVersion\":\"1.0.0\",\"network\":{\"type\":\"ethernet\",\"macAddress\":\"00:30:AB:FF8:0E\",\"ipAddress\":\"\"},\"swLastUpdatedTime\":\"1542008395\",\"pcsModel\":\"Mario 6.0 \",\"inverterHwVer\":\"00.02\",\"inverterSwVer\":\"\",\"time\":{\"timeZone\":\"8\",\"utcTimeCmd\":\"1545369829\"},\"batteryInfo\":[{\"batteryModel\":\"\",\"batterySerial\":\"0\",\"batteryCapacity\":\"9800\"},{\"batteryModel\":\"\",\"batterySerial\":\"0\",\"batteryCapacity\":\"0\"}],\"pcsOutLimit\":\"6000\",\"pcsOpMode\":\"0\",\"TOU\":{\"chargeStart\":\"19\",\"chargeEnd\":\"30\",\"chargePower\":\"40\",\"dischargeStart\":\"50\",\"dischargeEnd\":\"60\",\"dischargePower\":\"300\"},\"backupSOC\":\"30\",\"gridSelector\":{\"gridType\":\"17\",\"utility\":\"3\"},\"gridStatus\":\"0\",\"rule21\":{\"synchronization\":{\"syncTime\":\"500.00\",\"vLow\":\"89.0\",\"vHigh\":\"105.0\",\"freqLow\":\"59.66\",\"freqHigh\":\"61.00\"},\"ramp\":{\"softStartRamp\":\"888.0\",\"normalRamp\":\"888.0\"},\"lhvrt\":{\"ov2Voltage\":\"120\",\"ov2Time\":\"0.16\",\"ov1Voltabe\":\"115\",\"ov1Time\":\"12.00\",\"uv1Voltage\":\"88\",\"uv1Time\":\"50.00\",\"uv2Voltage\":\"80\",\"uv2Time\":\"20.00\",\"uv3Voltage\":\"50\",\"uv3Time\":\"0.16\"},\"lhfrt\":{\"of2Freq\":\"64.50\",\"of2Time\":\"2.00\",\"of1Freq\":\"63.00\",\"of1Time\":\"21.00\",\"uf1Freq\":\"58.88\",\"uf1Time\":\"21.00\",\"uf2Freq\":\"56.00\",\"uf2Time\":\"22.00\"},\"powerPriority\":\"0\",\"activePowerControl\":{\"mode\":\"1\",\"fixP\":{\"maxPower\":\"99.0\"},\"v1\":\"105.0\",\"p1\":\"100.0\",\"v2\":\"109.0\",\"p2\":\"0.0\",\"voltWatt\":{\"timeConst\":\"100\"},\"freqWatt\":{\"hyst\":\"1\",\"startFreq\":\"2.00\",\"endFreq\":\"3.00\",\"gradient\":\"600.0\",\"timeConst\":\"80\"}},\"reactivePowerControl\":{\"mode\":\"1\",\"fixPf\":{\"cosPhi\":\"-0.950\"},\"voltVar\":{\"v1\":\"110.0\",\"q1\":\"100.0\",\"v2\":\"110.0\",\"q2\":\"100.0\",\"v3\":\"110.0\",\"q3\":\"100.0\",\"v4\":\"100.0\",\"q4\":\"-44.0\",\"timeConst\":\"100\"}}}}",
"online": 1
}
]
}

直接用正则
cmd_id\":\(.+?)\"

其实我建议 beanshell 会更灵活一点
JSONObject,JSONArray 了解下
如果不会,我明天上班给你写

感谢各位的回复,问题之前已经百度解决了,一直没有上来回复,下面是我的解决方案

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;


//BeanShell中直接读取Response并提取
String response = prev.getResponseDataAsString();
JSONObject response_jso = JSON.parseObject(response);
JSONArray data = response_jso.getJSONArray("data");
log.info("data:"+data);
JSONObject data_29083 = (JSONObject)data.getJSONObject(0);
log.info("data_29083:"+data_29083);
JSONObject config = data_29083.getJSONObject("config");
log.info("config:"+config);
String cmd_id = config.getString("cmd_id");
vars.put("cmd_id",cmd_id);
log.info("cmd_id:"+cmd_id);
猫大白 关闭了讨论 04月08日 18:53
猫大白 重新开启了讨论 04月08日 18:54

楼主可以分享下你的
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;吗?遇到同样的问题可惜暂时写不出来

3Q

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册