简体   繁体   English

使用jQuery或javascript遍历json调用中的每个obj

[英]Loop through each obj in a json call using jQuery or javascript

I'm quite new to json/jQuery and javascript so I may be doing something glaringly stupid, so go easy on me. 我是json / jQuery和javascript的新手,所以我可能做的事情非常愚蠢,所以请放轻松。 Also, I HAVE looked at questions asking similar things to what I'm asking, but I couldn't seem to get any of them to work for me. 另外,我也曾问过一些问题,这些问题与我要问的问题类似,但我似乎无法让其中任何一项为我工作。

What I am trying to do, is call json data from an API and get the "image.full" property for each object. 我正在尝试从API调用json数据,并为每个对象获取“ image.full”属性。 The included JSfiddle shows what I am trying to accomplish, although I have statically assigned to get the image of a single character(object) "Aatrox" in this case. 所包含的JSfiddle显示了我要完成的工作,尽管在这种情况下,我已经静态分配以获取单个字符(对象)“ Aatrox”的图像。 I have supplied sample data for two characters(objects) "Thresh" and "Aatrox" 我提供了两个字符(对象)“ Thresh”和“ Aatrox”的样本数据

Sample json data: 样本json数据:

{
 "data": {
   "Thresh": {
     "id": "Thresh",
     "title": "the Chain Warden",
     "name": "Thresh",
     "image": {
        "w": 48,
        "full": "Thresh.png",
        "sprite": "champion3.png",
        "group": "champion",
        "h": 48,
        "y": 0,
        "x": 48
     },
     "key": "412"
  },
  "Aatrox": {
     "id": "Aatrox",
     "title": "the Darkin Blade",
     "name": "Aatrox",
     "image": {
        "w": 48,
        "full": "Aatrox.png",
        "sprite": "champion0.png",
        "group": "champion",
        "h": 48,
        "y": 0,
        "x": 0
     },
     "key": "266"
  },

HTML: HTML:

<div class="container-fluid">
    <div class="row" id="champs"></div>
</div>

jQuery: jQuery的:

$.getJSON('https://prod.api.pvp.net/api/lol/static-data/na/v1/champion?  champData=image&api_key=7d315bdf-c456-4792-b5d6-eadc7ef1672f', function (json) {
    var image = json.data.Aatrox.image.full;
    $('#champs').append('<div class="col-xs-4 col-md-1"><img   src="http://ddragon.leagueoflegends.com/cdn/4.3.18/img/champion/' + image + '"/></div>');
});

JSFIDDLE: http://jsfiddle.net/8S8LZ/2/ JSFIDDLE: http : //jsfiddle.net/8S8LZ/2/

Question Summary: How can I loop through and get the "image.full" property of each object within data (ie: Thresh, Aatrox)? 问题摘要:如何遍历数据并获取数据中每个对象的“ image.full”属性(即Thresh,Aatrox)? Also, I realize my API key is shown in this question, so I will be getting a new one after this is sorted out. 另外,我意识到我的API密钥已显示在此问题中,因此,在解决该问题后,我将获得一个新的密钥。 :) :)

Any help is greatly appreciate, thanks. 非常感谢任何帮助,谢谢。

This is quick and dirty, but if you loop through each object and access its image property in much the same way you're doing now, I modified your fiddle to create a list of objects: 这既快速又肮脏,但是如果您遍历每个对象并以与现在相同的方式访问其图像属性,那么我将修改您的小提琴以创建对象列表:

$.getJSON('https://prod.api.pvp.net/api/lol/static-data/na/v1/champion?champData=image&api_key=7d315bdf-c456-4792-b5d6-eadc7ef1672f', function (json) {
    $.each(json.data, function(ix, obj) {

var image = obj.image.full;
$('#champs').append('<div class="col-xs-4 col-md-1"><img src="http://ddragon.leagueoflegends.com/cdn/4.3.18/img/champion/' + image + '"/></div>');
});

});

Here is my version of your fiddle: http://jsfiddle.net/dshell/zfF8u/ 这是我的提琴版本: http : //jsfiddle.net/dshell/zfF8u/

Let's break this apart: 让我们分开:

  • First, we need to translate the object properties to an array, this is done with Object.keys 首先,我们需要将对象属性转换为数组,这是通过Object.keys完成的
  • Then, we can perform property access and return each full property, this is done with Array::map 然后,我们可以执行属性访问并返回每个full属性,这是通过Array::map

Something like 就像是

var images = Object.keys(json.data).map(function(key){
    return json.data[key].image.full
});

(Fiddle) (小提琴)

Try this: 尝试这个:

for (var prop in json.data) {
    if (json.data.hasOwnProperty(prop)) {
        console.log(prop.image.full);
    }
}
for (var k in json.data) {
    var image = json.data[k].image.full;
    $('#champs').append('<div class="col-xs-4 col-md-1"><img   src="http://ddragon.leagueoflegends.com/cdn/4.3.18/img/champion/' + image + '"/></div>');
}

You can simply use jquery like so: 您可以像这样简单地使用jquery:

$.each(data, function (key, value) {
    console.log(value.image.full);
});

See a demo . 观看演示

You can simply assign the json.data to a variable and loop through it using jquery's each loop 您可以简单地将json.data分配给变量,然后使用jquery的each循环遍历它

var data = json.data;
$.each(data, function (key, value) {
    var full = value.image.full;  //alternatively value['image']['full'];

    // do whatever you want with full variable
});

This is how i would do it. 这就是我会做的。 Loop through the response, and then append it. 循环浏览响应,然后附加它。

You want to save the selector in a variable because its faster than querying the dom each time in the loop. 您希望将选择器保存在变量中,因为它比每次循环中查询dom都要快。

Also here is a fork of your jsfiddle: http://jsfiddle.net/LwhT6/1/ 这也是您的jsfiddle的分支: http : //jsfiddle.net/LwhT6/1/

$.getJSON('https://prod.api.pvp.net/api/lol/static-data/na/v1/champion?champData=image&api_key=7d315bdf-c456-4792-b5d6-eadc7ef1672f', function (json) {
    var $champContainer = $("#champs")

    for (var key in json.data) {
        var champ = json.data[key]
         ,  image = champ.image.full

        $champContainer.append('<div class="col-xs-4 col-md-1"><img src="http://ddragon.leagueoflegends.com/cdn/4.3.18/img/champion/' + image + '"/></div>')
    }
});

Can you try this ? 你可以试试这个吗?

$.getJSON('https://prod.api.pvp.net/api/lol/static-data/na/v1/champion?champData=image&api_key=7d315bdf-c456-4792-b5d6-eadc7ef1672f', function( json ) {
    var image = json.data.Aatrox.image.full;    
    console.log(json);
    $.each(json.data,function(key,value){
        var name=value.image.full;
         $('#champs').append( '<div class="col-xs-4 col-md-1"><img src="http://ddragon.leagueoflegends.com/cdn/4.3.18/img/champion/' + name + '"/></div>');
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM