JSON解析不会使用JavaScript执行

时间:2015-06-18 10:55:16

标签: javascript json list parsing

这是我目前的代码,任何帮助将不胜感激。我需要调用我的listApp.json文件并解析它。我想显示当前有一个链接的列表。我是新来的。

<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>   

1 个答案:

答案 0 :(得分:0)

由于data是一个对象,您可以使用games访问data.listApp.games数组并使用$.each()进行迭代,然后在循环回调中您将获得游戏对象作为第二个参数,您可以使用成员运算符

访问其属性
function jsonParser(data) {

    $.each(data.listApp.games, function (i, game) {
        var name = game.name;
        var url = game.url;
        var id = game.id;

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

    });

}