使用下面的代码,我在div(名为remoteFeed
)
说它得到JSON数组的字符串表示:遍历JSON数组串
{"id":"25","name":"Basil","country":"USA"}
我的问题是如何才能得到JSON数组元素?
P1请注意,即使它显示在div中的值 - 警报(见下文)显示空白!
alert(data); //-------------------Shows blank
注意:作为新手,我试图让价值第一。但在这种情况下,我们必须将其转换为json对象。所以,如果你知道一个更好的办法,让我在客户端是更明智
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
(function ($) {
$(document).ready(function(){
$('#remoteFeed').load('http://testservice/json/');
var data = remoteFeed.innerHTML;
alert(data); //-------------------Shows blank
/*
var jsonObj = JSON.stringify(eval("(" + data + ")"));
for(var i in jsonObj)
{
var id = data[i].country_code;
alert(id);
}
*/
});
})(jQuery);
var data = remoteFeed.innerHTML;
alert(data);
</script>
</head>
<body>
<div id="remoteFeed"></div>
</body>
PL提供完整的代码 - 而不是代码片段 –
我认为你需要使用'回调函数。 load()' – Satpal