开发者

JSON: How do I select an array inside another array?

开发者 https://www.devze.com 2023-03-07 07:11 出处:网络
I have the current JSON file: [{\"id\":\"1\",\"images\":[{\"img_id\":\"1\"},{\"img_id\":\"2\"},{\"img_id\":\"3\"}]},

I have the current JSON file:

[{"id":"1","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]},
[{"id":"2","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]}

How do I select the array with ID 1 and list every 'img_id' inside it, without repeating it for the other array?

edit 1: I am trying to parse it like this but this code is erroneous:

$("#button").cli开发者_如何学Cck(function() {
$.getJSON("../path/to/json", function(data) {
$.each(data[0].images.img_id, function(i,data){
var new_data ="<p src='path/to/folder/"+images.img_id+"'></p>";             
$(new_data).appendTo("#htmlTag");
});
}); return false;
});

Much appreciated.


You have an array of objects, where each object contains another array of objects. I'm assuming the JSON structure you are using is:

var a = [{"id":"1","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]},
         {"id":"2","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]}];

I'm also assuming you are using JavaScript.


PostEdit:

Your code is fairly close, I believe what I have below should work:

$("#button").click(function() {
    $.getJSON("../path/to/json", function(data) {
        $.each(data[0].images, function(i,data){
            var new_data ="<p src='path/to/folder/" + data.img_id + "'></p>";             
            $(new_data).appendTo("#htmlTag");
        });
    }); 

    return false;
});

All I did was change the first parameter to your each call from: data[0].images.img_id to: data[0].images. Also, I changed the declaration of new_data from:

var new_data ="<p src='path/to/folder/"+images.img_id+"'></p>"; 

to:

var new_data ="<p src='path/to/folder/"+data.img_id+"'></p>";

Note that the parameter "data" in the each callback function is simply the element in the array, while "i" is the index of that element in the array. Therefore, data is an object which looks like this:

{"img_id":1}

So, you can get the ID via data.img_id. Hope this helps.


I think you're talking about references. I don't think they are possible in JSON. In case you strictly need them and still want the readable serialization of your objects - I'd suggest you to look into YAML


I think there is a typo in your JSON...it seems like a bracket is missing at the end, and one has been added at the start of line 2. But, assuming you meant this (and are using Javascript):

var myJson = [{"id":"1","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]},
             {"id":"2","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]}];

then you can access whatever you need just like a nested Javascript object. If you wanted to only access the object with ID equal to 1, and order is not guaranteed, you would have to iterate:

for(var i = 0; i < myJson.length; i++){
    if(myJson[i].id === "1"){
        var imgs = myJson[i].images;
        for(var j = 0; j < imgs.length; j++){
            //do what you want with imgs[j].img_id
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号