zyx407703809
2017-03-25 04:18
采纳率: 50%
浏览 2.2k

JS里取后台传来的复杂JSON值的问题

求助!这样的问题遇到好几次了,都没解决掉过。报错信息在2楼

  $(function() {
            $.ajax({
                cache: true,
                type: "POST",
                url:'${path}/userorgController/orgListAndCountAllDengdaishouquan',
                async: false,
                success: function(data) {
                    var html="";
                    var html2="";
                    $("#shangjijiedian").empty();
                    $("#xiajijiedian").empty(); 
                    var orgList=data.data.orgList;
                    var countAllDengdaishouquanList=data.data.countAllDengdaishouquanList;
                    for (var i = 0, j = orgList.length; i < j; i++){
                        if(orgList[i].pid==0){  
                                html +='<div class="col-sm-3" style="margin-left:38%;">'
                                html +='<div class="widget red-bg p-lg text-center">'
                                html +='<div class="m-b-md">'
                                html +='<i class="fa fa-user-md fa-4x"></i>'
                                html +='<h2 class="m-xs">&nbsp;</h2>'
                                html +='<h2 class="font-bold no-margins">'
                                html +=orgList[i].orgname
                                html +='</h2>'
                                html +='</div>'
                                html +='</div>'                                               
                                html +='</div>'
                        }
                        if(orgList[i].pid==1){
                                html2 +='<div class="widget navy-bg p-lg text-center w100">'
                                html2 +='<span class="badge badge-danger">'
                                if(countAllDengdaishouquanList[i].userorgtemp17!=0){                        
                                html2 +=countAllDengdaishouquanList[i].userorgtemp17;
                                }
                                html2 +='</span>'
                                html2 +='<div class="m-b-md fontlink_zu">'
                                html2 +='<h2 class="font-bold no-margins">'
                                html2 +='<a href="${path}/userorgController/listByPid?id='+orgList[i].id+'">'
                                html2 +=orgList[i].orgname
                                html2 += '</a>'
                                html2 +='</h2>'     
                                html2 +='</div>'
                                html2 +='</div>'
                        } 
                    }
                    $("#shangjijiedian").append(html);
                    $("#xiajijiedian").append(html2);
                }
            });
        });

下面是前台接收到的JSON

 [
    {
        "data": {
            "orgList": [
                {
                    "id": 1,
                    "orgname": "检验科",
                    "pid": 0
                },
                {
                    "id": 2,
                    "orgname": "临检与血液组",
                    "pid": 1
                },
                {
                    "id": 3,
                    "orgname": "生化组",
                    "pid": 1
                },
                {
                    "id": 4,
                    "orgname": "免疫组",
                    "pid": 1
                },
                {
                    "id": 5,
                    "orgname": "微生物组",
                    "pid": 1
                },
                {
                    "id": 6,
                    "orgname": "分子生物组",
                    "pid": 1
                },
                {
                    "id": 7,
                    "orgname": "门诊血液仪器镜检岗",
                    "pid": 2
                },
                {
                    "id": 8,
                    "orgname": "门诊体液镜检仪器岗",
                    "pid": 2
                },
                {
                    "id": 9,
                    "orgname": "病房血液仪器镜检岗",
                    "pid": 2
                },
                {
                    "id": 10,
                    "orgname": "病房体液仪器镜检岗",
                    "pid": 2
                },
                {
                    "id": 11,
                    "orgname": "儿科病房采血岗",
                    "pid": 2
                },
                {
                    "id": 12,
                    "orgname": "血液病房采血岗",
                    "pid": 2
                },
                {
                    "id": 13,
                    "orgname": "凝血检测岗",
                    "pid": 2
                },
                {
                    "id": 14,
                    "orgname": "急诊临检仪器岗",
                    "pid": 2
                },
                {
                    "id": 15,
                    "orgname": "急诊采血窗口岗",
                    "pid": 2
                },
                {
                    "id": 16,
                    "orgname": "流式细胞术检测岗",
                    "pid": 2
                },
                {
                    "id": 17,
                    "orgname": "血液流变检测岗",
                    "pid": 2
                },
                {
                    "id": 18,
                    "orgname": "脱落细胞检测岗",
                    "pid": 2
                },
                {
                    "id": 19,
                    "orgname": "操作仪器岗",
                    "pid": 3
                },
                {
                    "id": 20,
                    "orgname": "电泳岗",
                    "pid": 3
                },
                {
                    "id": 21,
                    "orgname": "急诊生化岗",
                    "pid": 3
                },
                {
                    "id": 22,
                    "orgname": "前处理岗",
                    "pid": 3
                },
                {
                    "id": 23,
                    "orgname": "微量元素岗",
                    "pid": 3
                },
                {
                    "id": 24,
                    "orgname": "杂项岗",
                    "pid": 3
                },
                {
                    "id": 25,
                    "orgname": "操作仪器岗",
                    "pid": 4
                },
                {
                    "id": 26,
                    "orgname": "手工杂项岗",
                    "pid": 4
                },
                {
                    "id": 27,
                    "orgname": "自身抗体检测岗",
                    "pid": 4
                },
                {
                    "id": 28,
                    "orgname": "前处理岗",
                    "pid": 4
                },
                {
                    "id": 29,
                    "orgname": "试剂准备及涂片岗",
                    "pid": 5
                },
                {
                    "id": 30,
                    "orgname": "细菌鉴定岗",
                    "pid": 5
                },
                {
                    "id": 31,
                    "orgname": "真菌鉴定岗",
                    "pid": 5
                },
                {
                    "id": 32,
                    "orgname": "标本接收岗",
                    "pid": 5
                },
                {
                    "id": 33,
                    "orgname": "样本分析理\\检测岗",
                    "pid": 6
                },
                {
                    "id": 34,
                    "orgname": "结果报告\\解释岗",
                    "pid": 6
                },
                {
                    "id": 78,
                    "orgname": "asda驱12驱蚊器",
                    "pid": 1
                },
                {
                    "id": 82,
                    "orgname": "121212121",
                    "pid": 78
                },
                {
                    "id": 83,
                    "orgname": "sdfs",
                    "pid": 1
                },
                {
                    "id": 85,
                    "orgname": "而且为其",
                    "pid": 1
                }
            ],
            "countAllDengdaishouquanList": [
                {
                    "userorgtemp16": 1,
                    "userorgtemp17": 1
                },
                {
                    "userorgtemp16": 2,
                    "userorgtemp17": 2
                },
                {
                    "userorgtemp16": 3,
                    "userorgtemp17": 2
                },
                {
                    "userorgtemp16": 4,
                    "userorgtemp17": 1
                },
                {
                    "userorgtemp16": 5,
                    "userorgtemp17": 1
                },
                {
                    "userorgtemp16": 6,
                    "userorgtemp17": 0
                },
                {
                    "userorgtemp16": 7,
                    "userorgtemp17": 3
                },
                {
                    "userorgtemp16": 8,
                    "userorgtemp17": 0
                }
            ]
        }
    }
]
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 已采纳

    orgList长度明显大于countAllDengdaishouquanList的长度了。。下面的代码下标越界了,自己搞清楚逻辑来显,不想报错加个越界判断

                                     if(countAllDengdaishouquanList[i].userorgtemp17!=0){                        
                                    html2 +=countAllDengdaishouquanList[i].userorgtemp17;
                                    }
    
                                                                ==》
                                                                if(countAllDengdaishouquanList[i]&&.userorgtemp17!=0){                        
                                    html2 +=countAllDengdaishouquanList[i].userorgtemp17;
                                    }
    
    打赏 评论
  • zyx407703809 2017-03-25 04:20

    图传错了,是这张图片说明

    打赏 评论
  • 当作看不见 2017-03-25 05:11

    报错 是由于你没有做异常处理,导致后面的全部没有执行
    if(count.....[i]!=undefined&&count...[i].userorgtemp17!=undefined){
    这里再执行你的处理代码 。
    }

    打赏 评论

相关推荐 更多相似问题