如何遍历一组值对 JSON 对象


How To Loop Through Set Of Value Pairs JSON Object

>im 试图通过 Jquery 遍历 JSON 对象。由于某种原因,它没有循环正确。它似乎一直循环到最后,但我想在我的对象中获取每个单独的属性。我使用 For(var in) 循环,它可以正确循环我的对象,但它有点偏离。我的任何帮助将不胜感激。非常感谢!!如果需要,我可以提供指向我的网站的快速链接,该网站具有代码模型。我还通过使用..提示*** 还有更多如果 - 检查sounds_like,sounds_price的条件语句... 第一个 JSON 对象工作没有问题,它是我与弹出窗口一起使用的第二个 JSON 对象,这给我带来了麻烦

    <div class="overlay-bg">
         <div id= "overlay" class="overlay-content">
             <p>This is a popup!</p>
          <button class="close-btn">Close</button>
        </div>
    </div>

        $.getJSON("php/music_data.php",function(data){
             $.each(data,function(key,obj){
                 for(var prop in obj){
                     // console.log("Property: " + prop + " key:" + obj[prop]);
                     if(prop === "sounds_like"){
                         results_div =  document.getElementById("results");
                         music_div_container = document.createElement("div");
                         music_div_container.id  = "music_data_container";
                         music_div_container.innerHTML = "<div id='"sounds_like'">" + "Sounds Like: "  + obj["sounds_like"] +"</div>";
                         results_div.appendChild(music_div_container);
                         var pop_up = document.createElement("a");
                         pop_up.href = "#";
                         pop_up.className = "show-popup";
                         pop_up.innerHTML = "Click";
                         music_div_container.appendChild(pop_up);
                         default_photo = document.createElement('div');

                     }
                     if(prop === "sound_desc"){
                         var sound_desc = document.createElement("div");
                         sound_desc.innerHTML  = "<div id='"sounds_desc'">" +        obj["sound_desc"] +"</div>";
                         music_div_container.appendChild(sound_desc);
                     }

     $.getJSON("php/music_data.php",function(data){
   $.each(data,function(idx,obj){
    $.each(obj,function(key,value){
        $(".show-popup").click(function(event){
            event.preventDefault();     
            $(".overlay-bg").show();
              if(key === "sounds_like"){
                          /***Should be Beyonce,Drake,Nicki Minaj***/
                          /*****But my console is showing Nicki Minaj*******/
                  $(".overlay-content").html(value);
              }
            if(value === "sounds_desc"){
                           /***Should be Smooth, Wu tang Forever, Barbie***/
                          /*****But my console is showing Barbie*******/
                $(".overlay-content").html(value);
             }

            $('.close-btn').click(function(){
                $('.overlay-bg').hide(); /*** hide the overlay ***/
            });
            $('.overlay-bg').click(function(){
                $('.overlay-bg').hide(); 
            });
            $('.overlay-bg').click(function(){
                return false;
            })
        });
    });
   })
 });

下面的 JSON 对象

[{"id":"39","sounds_like":"Beyonce","sound_name":"Dance 4 u.mp3","sound_desc":"Smooth","sound_genre":"R&B","sound_price":"N/A","photo_path":"'/admin_data'/uploaded_artist_photos'/","photo_name":"beyonce.jpg"},
 {"id":"40","sounds_like":"Drake","sound_name":"Bottom.mp3","sound_desc":"Wu Tang Forever","sound_genre":"Rap","sound_price":"N/A","photo_path":"'/admin_data'/uploaded_artist_photos'/","photo_name":"drake.jpg"},
 {"id":"41","sounds_like":"Nicki Minaj","sound_name":"RomanReloaded.mp3","sound_desc":"Barbie","sound_genre":"Rap","sound_price":"N/A","photo_path":"'/admin_data'/uploaded_artist_photos'/","photo_name":"nickiminaj.jpg"}
]

当您使用 for var in 循环循环复杂对象时,由于此循环的工作方式,您总是会遇到意外的行为。

为了避免此类问题,如果您需要使用这种类型的循环,我建议您执行以下操作:

例:

for (var i in obj) {
    if (obj.hasOwnProperty(i)) {  // this validates if prop belongs to obj 
        // do something
    }
}

编辑 1:

我不确定你想做什么,但使用 jquery 你可以执行以下操作:

$.getJSON("php/music_data.php", function (data) {
    $.each(data, function (i, value) {
        //alert(i + ": " + value.id);
        alert(value.sounds_like); 
        // this will have Beyonce , Drake, Nicki Minaj
    });
});

另一件似乎不对的事情是,您正在每个循环上执行绑定单击事件。以不同的方式这样做是否更好。