weixin_33697898 2017-08-30 18:54 采纳率: 0%
浏览 25

将JSON数据解析为HTML

I am trying to output the results of a data.json file, but I keep getting the wrong output. at first I was getting [obj obj], but I tried this method, and it seems to be getting the index.

I even removed the outside curly braces and the games title from the JSON file, but still not working. At the moment, I am just trying to console the game title, but the idea is to output the whole thing.

//my JSON is in a different file

{
  "games" : [
    {
      "gameImg"   : "img/deathstranding.jpg",
      "gameTitle" : "Death Stranding",
      "gameInfo"  : "some game info"
    },
    {
      "gameImg"   : "img/deathstranding.jpg",
      "gameTitle" : "Death Stranding",
      "gameInfo"  : "some game info"
    }
  ]
}

// JS

function init2() {
    $.ajax({
        url: "js/data.json",
        type: "GET",
        dataType: "json",
        async: false,
        success: function (data) {
          JSON.stringify(data);
          $.each(data, function( val ) {
          var items = [];
          items.push( "<li>" + val + "</li>" );
          console.log('gameTittle:', items);
        });
      }
    });
}
init2();
  • 写回答

1条回答 默认 最新

  • 叼花硬汉 2017-08-30 19:02
    关注

    $.each(data, function( val ) {}

    Here you are making a mistake, first parameter inside $.each function is an index and second is actual value, change it to:

    $.each(data, function( index, val ) {...}

    Documentation: jQuery.each()

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度