浏览json嵌套文件

Navigating through a json nested file

本文关键字:文件 嵌套 json 浏览      更新时间:2023-09-26

需要一些帮助导航通过json嵌套文件。我不是jQuery的专家,所以如果你能解释一下它是如何工作的,我将不胜感激。

$(window).load(function(){  
    $.each(data.videos, function(entryIndex, entry) {
        var html = $('#name').append($('<div>', {text: this.name})) + $('#country').append($('<div>', {text: this.country})) + $('#brands').append($('<div>', {text: this.brands[0] + this.brands[1]}));
        $.each(this.brands, function() {
           $('#views').append($('<li>'+ {text: this.views} + '</li>'));
           $('#shares').append($('<li>'+ {text: this.shares} + '</li>'));
           $('#likes').append($('<li>'+ {text: this.likes} + '</li>'));
        }); 
    });
});

查找示例:http://jsfiddle.net/epgBz/

除非你将文件加载为普通字符串,否则你不是在导航"json文件",你只是从文件中加载了一个JavaScript对象,现在你像访问任何其他JavaScript对象一样访问JavaScript对象,所有常规JavaScript规则都适用:Object.keys(yourobj)获取所有键作为数组,并遍历对象中的所有键/值对:

var keys = Object.keys(yourobject);
keys.forEach(function(key) {
  var value= yourobject[key];
  console.log(key, ":", value);
  // ... more code here ...
});

或者jQuery的方式,

$.each(yourobject, function(key, value) {
   console.log(key, ":", value);
   // ... more code here ...
});