【问题标题】:parse json response in Loadrunner在 Loadrunner 中解析 json 响应
【发布时间】:2015-01-29 05:33:13
【问题描述】:

在 loadrunner 脚本中,我想解析收到的 json 响应并检查响应是否正确。为此,我有以下代码:

web_set_max_html_param_len("999999");

web_custom_request("JsonData", 
    "URL=https://localhost:8080/testpgm", 
    "Method=POST", 
    "RecContentType=application/json", 
      LAST);

web_reg_save_param("JsonData",
    "LB=",
    "RB=",
    "Search=Body",
    LAST);

web_js_run(
"Code=getEmployee(LR.getParam('JsonData'));",
"ResultParam=Names", 
SOURCES, 
"File=getEMP.js",ENDITEM, 
LAST); 

javascript代码如下:

function getEmployee(stringData)
{ 
    var data = JSON.parse(stringData); 
    var index = data[data.length-1];
    var value = data.ItemLists{0}.Items{0}.NamedValues{0}.Value{1}.name;
    return value; 
} 

它不起作用,我收到以下错误:

Action.c(43): web_js_run started    [MsgId: MMSG-26355]
Action.c(43): Error -26000: Error from JS Engine: "C:\Users\admin\Documents\VuGen\Scripts\Web1\getEMP.js:5:SyntaxError: missing ; before statement
"   [MsgId: MERR-26000]
Action.c(43): Error -35051: Failed to run the JavaScript code   [MsgId: MERR-35051]
Action.c(43): Warning -26379: Pending web_reg_save_param/reg_find/create_html_param[_ex] request(s) are deleted and will be handled as "not found"      [MsgId: MWAR-26379]
Action.c(43): Error -26377: No match found for the requested parameter "JsonData". Check whether the requested boundaries exist in the response data. Also, if the data you want to save exceeds 999999 bytes, use web_set_max_html_param_len to increase the parameter size    [MsgId: MERR-26377]
Action.c(43): Error -26374: The above "not found" error(s) may be explained by header and body byte counts being 0 and 0, respectively.     [MsgId: MERR-26374]
Action.c(43): web_js_run highest severity level was "ERROR"     [MsgId: MMSG-26391]

【问题讨论】:

  • 您在这里尝试做什么:“var value = data.ItemLists{0}.Items{0}.NamedValues{0}.Value{1}.name;”?我认为您的意思是 var value = data.ItemLists[0].Items[0].NamedValues[0].Value[1].name;
  • 是的。没错。
  • 你能发布你的jsondata吗?您可以尝试 chrome 中的 js 函数,看看它的功能是否正确。像这样的简单 html 文件可以帮助你做到这一点

标签: javascript json parsing loadrunner


【解决方案1】:

相关函数(web_reg_save_param)应该放在请求之前。 试试

web_set_max_html_param_len("999999");

web_reg_save_param("JsonData",
    "LB=",
    "RB=",
    "Search=Body",
    LAST);


web_custom_request("JsonData", 
    "URL=https://localhost:8080/testpgm", 
    "Method=POST", 
    "RecContentType=application/json", 
      LAST);


web_js_run(
"Code=getEmployee(LR.getParam('JsonData'));",
"ResultParam=Names", 
SOURCES, 
"File=getEMP.js",ENDITEM, 
LAST); 

【讨论】:

    猜你喜欢
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    相关资源
    最近更新 更多