post请求获取json数据 解析json数据

    <script>
        window.onload = function () {
            var str;
           // console.log(@ViewBag.ID);
            $.post("/ServiceBills/ServiceBills/UpdateA",
                { id: @ViewBag.ID},
                function (data) {
                    for (var attr in data) {
                       // console.log(attr);//name,age,gender
                        //console.log(data[attr]);//jack,25,male  //值
                        //console.log(typeof json1[attr]);
                        // $("#" + attr).val("111111111");

                        $("#" + attr).val(data[attr]);  //id

                        if ((data[attr] + "").indexOf("/Date(") != -1) {  //时间
                            str = data[attr] + "";
                            str = timestampToTime(str.substring(6, str.length - 2)); //转换时间
                          //  console.log(str);
                        }
                        else {
                            str = data[attr];
                        }
                        if ((attr + "").indexOf("workResult") != -1) { //包含workResult字符串
                            console.log("++++");
                            str = null;                          
                        }
                        if ((attr + "").indexOf("UserSatisfaction") != -1) {
                            console.log("----");
                            str = null;
                        }
                        if (str != null) {
                            $("input[name='" + attr + "']").val(str); //输入框   
                        }
                       

                        //复选框
                        if ((attr + "").indexOf("demandType") != -1) {  //包含该字符串
                            var strs = (data[attr] + "").split(",");  //输出这个字符串分割为数组
                            //获取集合
                            var objs = document.getElementsByName("demandType[]"); //获得对象集合
                           // console.log(objs);
                            for (var i = 0; i < objs.length; i++) {
                                for (var j = 0; j < strs.length; j++) {
                                    if (strs[j].indexOf(objs[i].value) > -1) { //判断对象的值是否在这个数组中
                                       // console.log(strs[j]);
                                        objs[i].checked = true;       //选中复选框                                
                                    }
                                };
                            }
                        }
                        if ((attr + "").indexOf("userDemand") != -1) {  //包含该字符串
                            var strs = (data[attr] + "").split(",");  //输出这个字符串分割为数组
                            //获取集合
                            var objs = document.getElementsByName("userDemand[]"); //获得对象集合
                           // console.log(objs);
                            for (var i = 0; i < objs.length; i++) {
                                for (var j = 0; j < strs.length; j++) {
                                    if (strs[j].indexOf(objs[i].value) > -1) { //判断对象的值是否在这个数组中
                                        //console.log(strs[j]);
                                        objs[i].checked = true;       //选中复选框                                
                                    }
                                };
                            }
                        }

                        //单选框
                       // workResult
                        if ((attr + "").indexOf("workResult") != -1){
                            var str0 = data[attr] + "";
                            var objs = document.getElementsByName("workResult"); //获得对象集合
                           // console.log(objs);
                            for (var i = 0; i < objs.length; i++) {
                                //console.log(objs.length);                       
                                if (str0.indexOf(objs[i].value) > -1){
                                    objs[i].checked = true;
                                    console.log(objs[i].value);
                                }
                              
                            }
                        }
                        if ((attr + "").indexOf("UserSatisfaction") != -1) {
                            var str0 = data[attr] + "";
                            var objs = document.getElementsByName("UserSatisfaction"); //获得对象集合
                            for (var i = 0; i < objs.length; i++) {
                                if (str0.indexOf(objs[i].value) > -1) {
                                    objs[i].checked = true;
                                    console.log(objs[i].value);
                                }
                            }

                        }

                    }
                });
        }
        function timestampToTime(timestamp) {
            var date = new Date(timestamp * 1);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
            Y = date.getFullYear() + '-';
            M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            D = date.getDate() + ' ';
            h = date.getHours() + ':';
            m = date.getMinutes() + ':';
            s = date.getSeconds();
            return Y + M + D + h + m + s;
        }
    </script>

 

posted @ 2018-02-23 08:39  enych  阅读(5463)  评论(0编辑  收藏  举报