zyx407703809 2017-03-25 04:18 采纳率: 50%
浏览 2180
已采纳

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条回答 默认 最新

  • 斯洛文尼亚旅游 2017-03-25 05:08
    关注

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

                                     if(countAllDengdaishouquanList[i].userorgtemp17!=0){                        
                                    html2 +=countAllDengdaishouquanList[i].userorgtemp17;
                                    }
    
                                                                ==》
                                                                if(countAllDengdaishouquanList[i]&&.userorgtemp17!=0){                        
                                    html2 +=countAllDengdaishouquanList[i].userorgtemp17;
                                    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。