如何从下一个json数组节点获取值

时间:2017-09-08 05:41:40

标签: javascript jquery arrays json

我有一个JSON问题,也许有人可以帮助我。我有以下JSON数组,我称之为“showresults”:

    [  
   {  
      "UniqueCode":"98917",
      "NetworkUC":"c99af",
      "Name":"Born This Way",
      "ShowImage":"./assets/Born_This_Way_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"f583f",
      "NetworkUC":"c99af",
      "Name":"Bates Motel",
      "ShowImage":"./assets/Bates_Motel_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"66ede",
      "NetworkUC":"c99af",
      "Name":"60 Days In",
      "ShowImage":"./assets/60_Days_In_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"c3ecf",
      "NetworkUC":"c99af",
      "Name":"Intervention",
      "ShowImage":"./assets/Intervention_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"0cac8",
      "NetworkUC":"c99af",
      "Name":"Wahlburgers",
      "ShowImage":"./assets/Wahlburgers_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"a5866",
      "NetworkUC":"c99af",
      "Name":"Leah Remini: Scientology and the Aftermath",
      "ShowImage":"./assets/Leah_remini_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"263f5",
      "NetworkUC":"c99af",
      "Name":"L.A. Burning: The Riots 25 Years Later",
      "ShowImage":"./assets/LA_burning_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"804b6",
      "NetworkUC":"c99af",
      "Name":"Biggie: The Life of Notorious B.I.G.",
      "ShowImage":"./assets/Biggie_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"6a9ce",
      "NetworkUC":"c99af",
      "Name":"The Murder of Laci Petersen",
      "ShowImage":"./assets/Laci_paterson_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"b5513",
      "NetworkUC":"c99af",
      "Name":"Who Killed Tupac?",
      "ShowImage":"./assets/Tupac_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"29b95",
      "NetworkUC":"c99af",
      "Name":"Life, Animated",
      "ShowImage":"./assets/Life_animated_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"2d576",
      "NetworkUC":"c99af",
      "Name":"Live PD",
      "ShowImage":"./assets/Livepd_502x502.jpg",
      "ShowLogo":"null"
   }
]

我创建了2个span标记,用于显示“Name”节点值。我无法弄清楚的问题是,如果页面当前正在显示showresults [3]中的内容,我想要一个span标签显示showresults [2] .Name的值,另一个显示showresults [4] 。名称。这是我的代码:

if (showresults.length > 0){

        var first = showresults[0];
        var count = showresults.length;

        var toparrow = document.createElement("img");
        toparrow.setAttribute("class", "top_arrow");
        toparrow.setAttribute("src", "./images/top_arrow.jpg");
        toparrow.setAttribute("uc", UniqueCode);
        toparrow.addEventListener("click",LoadVideo);

        var downarrow = document.createElement("img");
        downarrow.setAttribute("class", "down_arrow");
        downarrow.setAttribute("src", "./images/down_arrow.jpg");
        downarrow.setAttribute("uc", UniqueCode);
        downarrow.addEventListener("click", LoadVideo);

        var backShow = document.createElement("span");
        backShow.setAttribute("class","back");
        backShow.setAttribute("uc", UniqueCode);
        backShow.addEventListener("click", LoadVideo);

        var forwardShow = document.createElement("span");
        forwardShow.setAttribute("class","forward");
        forwardShow.addEventListener("click", LoadVideo);

        for (var i = 0; i < showresults.length; i++) {

            if (i == 0){

                var curobj = showresults[i];
                var curobjnext = showresults[i++];
                var curobjprevious = showresults[i--];
                var UniqueCode = curobj["UniqueCode"];
                var Name = curobj["Name"];
                var NameNext = curobjnext["Name"];
                var NamePrevious = curobjprevious[Name];

                backShow.innerHTML = NamePrevious;
                forwardShow.innerHTML = NameNext;

                if(curobj == first){
                    $('#video').append(downarrow);
                    $('#video').append(forwardShow);
                } else {
                    $('#video').append(toparrow);
                    $('#video').append(downarrow);
                    $('#video').append(backShow);
                    $('#video').append(forwardShow);
                }


            }


        }


    }

“NameNext”和“NamePrevious”变量未返回预期结果。有人可以指出我正确的方向吗?

2 个答案:

答案 0 :(得分:1)

将其更改为:

if (showresults.length > 0){

    var first = showresults[0];
    var count = showresults.length;

    var toparrow = document.createElement("img");
    toparrow.setAttribute("class", "top_arrow");
    toparrow.setAttribute("src", "./images/top_arrow.jpg");
    toparrow.setAttribute("uc", UniqueCode);
    toparrow.addEventListener("click",LoadVideo);

    var downarrow = document.createElement("img");
    downarrow.setAttribute("class", "down_arrow");
    downarrow.setAttribute("src", "./images/down_arrow.jpg");
    downarrow.setAttribute("uc", UniqueCode);
    downarrow.addEventListener("click", LoadVideo);

    var backShow = document.createElement("span");
    backShow.setAttribute("class","back");
    backShow.setAttribute("uc", UniqueCode);
    backShow.addEventListener("click", LoadVideo);

    var forwardShow = document.createElement("span");
    forwardShow.setAttribute("class","forward");
    forwardShow.addEventListener("click", LoadVideo);

    for (var i = 0; i < showresults.length; i++) {

        if (i == 0){

            var curobj = showresults[i];
            var curobjnext = showresults[i+1];      // change here
            var curobjprevious = showresults[i-1];  // and here
            var UniqueCode = curobj["UniqueCode"];
            var Name = curobj["Name"];
            var NameNext = curobjnext["Name"];
            var NamePrevious = curobjprevious[Name];

            backShow.innerHTML = NamePrevious;
            forwardShow.innerHTML = NameNext;

            if(curobj == first){
                $('#video').append(downarrow);
                $('#video').append(forwardShow);
            } else {
                $('#video').append(toparrow);
                $('#video').append(downarrow);
                $('#video').append(backShow);
                $('#video').append(forwardShow);
            }


        }


    }


}

但我没有得到的是你只通过if (i == 0){显示第一个项目,这意味着在showresults[i-1] i中将是-1 ...所以要么无限通过showresults[showresults.length]或不显示以前的结果......

答案 1 :(得分:1)

人们没有告诉你你做错了什么:

i++是一个语句,它在返回原始值时递增变量,因此等于{i = i + 1, return i - 1},因此它实际上修改了i的值。所以这就是发生的事情:

var curobj = showresults[i]; // i was 3
// i == 3
var curobjnext = showresults[i++]; // i was 3
// i == 4
var curobjprevious = showresults[i--]; // i was 4
// i == 3

您不希望在进展过程中修改该值,因此i+1i-1是更合适的表达方式。