我想創建一個LoadRunner的「JS」文件從一個JSON捕獲一些數據。 json文件位於網絡服務器上,這不是問題。我可以成功檢索json。不過,我想從json文本中返回一個特定的值。我試圖做到這一點本地的HTML頁面上進行如下的,但我總是得到一個錯誤信息:創建LoadRunner的腳本來捕捉JSON數據
Error is TypeError: Cannot read property 'Location' of undefined
代碼:
<!doctype html> <head></head><script>
function getdata()
{
var jsontext = '{"Locations":{"Location":[{"@id":"3649","@latitude":"51.758","@longitude":"-1.576","@country":"England","@zoomLevel":"10","@obsZoomLevel":"5","@type":"Observing Site","@name":"Brize Norton","@geohash":"gcnyknk2h"},{"@id":"3","@latitude":"50.9503","@longitude":"-1.3567","@country":"England","@zoomLevel":"11","@type":"Airport and Heliport","@name":"Southampton Airport","@geohash":"gcp1c5hp4"}]}}';
var stringifiedjson = JSON.stringify(jsontext).replace(/@/g, '_'); //convert to JSON string
var data = JSON.parse(stringifiedjson);
json.innerHTML=data;
try{
var newlocation0 = data.Locations.Location[1]._id;
output.innerHTML=newlocation0;
return;
}
catch(err){
message.innerHTML = "(1)Error is " + err;
}
try{
var newlocation0 = data.Locations.Location[1]._id;
output.innerHTML=newlocation0;
return;
}
catch(err) {
message.innerHTML = message.innerHTML + "(2)Error is " + err;
}
try{
var newlocation0 = data.Locations.Location[0]._id;
output.innerHTML=newlocation0;
return;
}
catch(err) {
message.innerHTML = message.innerHTML + "(3)Error is " + err;
}
}
</script>
<body>
Error Messages:
<p id="message"></p>
<BR><BR>
JSON Input:
<p id="json"></p>
<BR><BR>
Output:
<p id="output"></p>
<button onclick="getdata()">Check</button>
<button onclick="location.reload();">Reload</button>
</body>
我已經試過的符號的不同組合[0]和[1]但我無法獲得JSON右側的輸出。
爲什麼'VAR stringifiedjson = JSON.stringify(jsontext)'?這會逃避所有'「',讓您的JSON不可分析。在此之後,如果你做一個JSON.parse,輸出將不會是一個JS對象,但只有一個字符串。更換零件'VAR stringifiedjson = JSON.stringify(jsontext) .replace(/ @/g,'_');'var by stringifiedjson = jsontext.replace(/ @/g,'_');' – Fefux
我已經開始工作了,感謝您的幫助!當我在印象之下進行串化時,這是解決json中不規則'@'的唯一方法。 – jquerynewbie
「性能測試工具LoadRunner」中的「LoadRunner」?如果是,則內置項目以處理相關性,捕獲從所述服務器向所述客戶端返回的數據的動作,這將包括任何類型的一個HTTP流內的數據(或其它協議)的。這也將包括JSON格式的數據。 –