我想從json數組中讀取id和名稱。我寫了下面的代碼,但是當我嘗試使用parseJSON方法讀取id時,它給了我未定義的代碼。任何人都可以告訴我如何閱讀json數據。以下是我寫的代碼:使用jquery讀取json數據
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type='text/javascript'>
$(document).ready(function() {
// $("#request_content").click(function(){
$('#time').html(new Date);
$('#status').html('');
$('#content').html('');
var id = '';
$.ajax({
cache: false,
url: $('#xhr_url').val()
}).done(function(data, textStatus, jqXHR) {
//alert(data);
$('#status').html(textStatus);
$('#content').html(JSON.stringify(data));
var jsonp = JSON.stringify(data);
var obj = $.parseJSON(jsonp);
$.each(obj, function() {
//alert("hi");
id += this['Id'] + "<br/>";
$('#content').html(id);
});
//$('#content').html(lang);
}).fail(function(jqXHR, textStatus) {
$('#status').html(textStatus);
$('#content').html('(failed)');
})
// });
});
</script>
<body>
<input id='xhr_url' style='width:600px;' type='text' value='http://t2json.tgoservices.com/818746/PrinterManufacturers'/>
<button id='request_content'>Request content</button>
<fieldset><legend>Time:</legend>
<div id='time'></div>
</fieldset>
<fieldset><legend>Status:</legend>
<div id='status'></div>
</fieldset>
<fieldset><legend>Content:</legend>
<div id='content'></div>
</fieldset>
</body>
請讓我知道問題是什麼。
向我們顯示JSON字符串。爲什麼你要連續兩行對數據進行串聯和解析? – freakish 2013-04-04 07:41:10