我需要能够检索并显示来自我正在制作的应用程序的网站的JSON数据。在将其应用到我的应用程序之前,我认为我应该确保通过在其他地方测试它了解它是如何工作的。我做了下面的HTML和JSON代码来测试它,但是如果我运行应用程序,我会得到Uncaught TypeError:无法读取属性'0'undefined在file:///android_asset/www/projectName.html:11 我是什么做错了?检索外部JSON数据
HTML:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$.get('testData.json', function(data) {
alert('get performed');
var obj = eval ("(" + data + ")");
$("p").html(obj.data_set[0].data1);
});
});
});
</script>
</head>
<body>
<h2>Heading</h2>
<p>Display</p>
<button>Click me</button>
</body>
</html>
JSON文件:
[{"data_set":{"data1":"string","data2":null,"data3":22.0}}]
你正在使用'eval',这是不必要的。我建议你阅读[JSON是什么以及如何使用它](http://www.copterlabs.com/blog/json-what-it-is-how-it-works-how-to-use-it/ ) – xdumaine 2012-07-30 19:36:58