這是我目前的代碼,任何幫助將不勝感激。我需要調用我的listApp.json文件並解析它。我想顯示我目前有一個鏈接的列表。我對此很陌生。JSON解析將不會執行與JavaScript
<script type = "text/javascript">
// If the .js files are linked correctly, we should see the following output inside the JavaScript Console
console.log("starting...");
// Gets the .json file and applies the function
var json;
// When the document is ready then run the function
$(document).ready(function(){
// Standard jQuery ajax technique to load a .json file
$.ajax({
type: "GET", // Requests data from a specified resource
url: "include/listApp.json", // Gets the file on which the url is directed to
dataType: "json", // Data Type needs to be json in this case. This can be xml
success: jsonParser // If successful then run the, 'jsonParser' function
});
});
// Actual parse function
function jsonParser(data) {
JSON = data;
$(JSON).find("games").each(function(){
games = $(this);
var name = $(games).find("name").text();
var url = $(games).find("url").text();
var id = $(ganes).find("id").text();
console.log(name);
// Appends list + link + name
$("#myList").append('<li>'+ "<a href ="+url+">"+name+"</a>"+'</li>');
$('#myList').listview('refresh');
$("#pages").append('<div data-role="page" id = "'+ id +'"><img src = '+ url +'> test</div>');
});
}
</script>
您是否收到錯誤消息?你可以粘貼lstApp.json的內容嗎? – scgough
'data'的值是什麼...如果'data'是一個json對象,那麼'$(JSON).find(「games」)'不會返回任何東西... –
這是我的.json文件: { 「listApp」:{ 「遊戲」:[{ 「ID」: 「1」, 「名」: 「輻射」, 「URL」:「http://vignette4.wikia.nocookie.net /fallout/images/e/e2/BoS_soldier_Capitol_building.jpg/revision/latest?cb=20090430094924" } ] }} 大多 –