【问题标题】:Fetch all data from json and display it on the index page从json中获取所有数据并显示在索引页面上
【发布时间】:2016-04-18 08:57:44
【问题描述】:

我有以下 json:

[
  {
    "countryId" : 0,
    "countryImg" : "../img/france.jpg",
    "countryName" : "France",
    "countryInfo" : {
      "headOfState" : "Francois Hollande",
      "capital" : "Paris",
      "population" : 66660000,
      "area" : 643801,
      "language" : "French"
    },
    "largestCities" : [
      {"Paris" : "../img/paris.jpg"},
      {"Marseille" : "../img/marseille.jpg"},
      {"Lyon" : "../img/lyon.jpg"}
    ]
  },

  {
    "countryId" : 1,
    "countryImg" : "../img/germany.jpg",
    "countryName" : "Germany",
    "countryInfo" : {
      "headOfState" : "Angela Merkel",
      "capital" : "Berlin",
      "population" : 81459000,
      "area" : 357168,
      "language" : "German"
    },
    "largestCities" : [
      {"Berlin" : "../img/berlin.jpg"},
      {"Munich" : "../img/munich.jpg"},
      {"Hamburg" : "../img/hamburg.jpg"}
    ]
  }
]

我需要把它放在我的 index.html 中,但是我不明白为什么我只得到第二个对象?我需要将两个对象放在索引中。也许我需要使用循环?我该如何正确地做到这一点? 我有以下 javascript 代码:

$.ajax({
    method: "POST",
    url: "../data/data.json"
}).done(function (data) {
    /*console.log(data);*/
    localStorage.setItem('jsonData', JSON.stringify(data));
    var dataFromLocStor = localStorage.getItem('jsonData');
    var dataObject = JSON.parse(dataFromLocStor);
    console.log(dataObject);

    function Countries(){

        this.getCountries = function () {
            var ulListElem = document.getElementById("list-of-teams").children,
                imgCountry = document.createElement('img');

            for(country in dataObject){
                /*console.log(dataObject[team]['teamName']);*/

                imgCountry.setAttribute('src', dataObject[country]['countryImg']);
                imgCountry.setAttribute("width", "400");
                imgCountry.setAttribute("height", "300");
                console.log(country);

                ulListElem[0].innerHTML = dataObject[country]['countryId'];
                ulListElem[1].innerHTML = dataObject[country]['countryName'];
                ulListElem[2].appendChild(imgCountry);
                ulListElem[3].innerHTML = dataObject[country]['countryInfo']['headOfState'];
                ulListElem[4].innerHTML = dataObject[country]['countryInfo']['capital'];
                ulListElem[5].innerHTML = dataObject[country]['countryInfo']['population'];
                ulListElem[6].innerHTML = dataObject[country]['countryInfo']['area'];
                ulListElem[7].innerHTML = dataObject[country]['countryInfo']['language'];
            }
        }
    }

    var countriesDate = new Countries();
    countriesDate.getCountries();
});

【问题讨论】:

    标签: javascript arrays json loops object


    【解决方案1】:

    您在一个循环中设置了两次相同的 UI 元素(img 和 ul)。当循环第一次运行时,值是从第一个数组元素设置的。当循环第二次运行时,SAME 元素将被新值覆盖。

    要正确显示 JSON 数组中的所有元素,您需要 index.html 页面中的两组 UI 元素,例如两个imgs,两个uls 等等。

    【讨论】:

    • 谢谢,我明白了。但也许它不是正确的让我做的事情吗?我可以建议如果在 json 中会有很多对象集,我应该如何正确地处理我的 index.html 文件。我认为创建 html 依赖 json 是不正确的,不是吗?也许你可以建议我另一种变体来完成这项任务?
    • 你可以循环遍历json数组来动态创建UI元素,否则你可以使用一些模板引擎比如Handlebars.js (handlebarsjs.com)从json动态生成HTML。
    • 非常感谢您的提示。我需要这样的东西。也许我应该更好地使用角度?我只为自己做这个任务,我想提高我的 javascript 技能,但也许我应该使用像 Angular 这样的框架?你怎么看?
    猜你喜欢
    • 2012-10-26
    • 2018-10-15
    • 2017-06-04
    • 2013-06-04
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    相关资源
    最近更新 更多