2016-12-19 21 views
0

我想創建一個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右側的輸出。

+0

爲什麼'VAR stringifiedjson = JSON.stringify(jsontext)'?這會逃避所有'「',讓您的JSON不可分析。在此之後,如果你做一個JSON.parse,輸出將不會是一個JS對象,但只有一個字符串。更換零件'VAR stringifiedjson = JSON.stringify(jsontext) .replace(/ @/g,'_');'var by stringifiedjson = jsontext.replace(/ @/g,'_');' – Fefux

+0

我已經開始工作了,感謝您的幫助!當我在印象之下進行串化時,這是解決json中不規則'@'的唯一方法。 – jquerynewbie

+0

「性能測試工具LoadRunner」中的「LoadRunner」?如果是,則內置項目以處理相關性,捕獲從所述服務器向所述客戶端返回的數據的動作,這將包括任何類型的一個HTTP流內的數據(或其它協議)的。這也將包括JSON格式的數據。 –

回答

1

jsontext最初是一個字符串

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, '_'); 

結果字符串看起來像這樣

""{\"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\"}]}}"" 

解析一次返回內JSON串

var data = JSON.parse(stringifiedjson); 

擺脫字符串對象解析再次

data = JSON.parse(data); 

OR

有擺在首位不需要JSON.stringify,你可以使用替換法。

+0

請問替換方法作爲工作我更換' 「?字符如果是的話我會怎麼做這 – jquerynewbie

+0

只是做jsontext.replace(/ @ /克,‘@ _’);而不是JSON.stringify(jsontext).replace(/ @/g,'_');我在控制檯上試了一下,它對我很有用 –

+0

這可以作爲一行代碼'這實際上可以是一行代碼'testjsonout.innerHTML =「0:」+(JSON.parse(replaceAll(jsontext,'@' 。, '_')))Locations.Location [0] ._ ID;' – jquerynewbie