用心做好每一件事情!

JavaScript 拼接JSON

    <script language="javascript" type="text/javascript">
        var json="";
        $(document).ready(function () {
            $("#Button1").bind("click", function () {
                $.ajax({
                    type: "post",
                    url: "GiftBox_RePrint.aspx?Oper=load_templateslist&key=" + Math.round(),
                    dataType: "text",
                    cache: false,
                    success: function (data) {
                        var arr_temp_list = new Array(); //返回的全部数据

                        var arr_temp_id = new Array(); //保存所有的模板ID
                        var arr_temp_name = new Array(); //保存所有的模板名称
                        arr_temp_list = data.split(","); //根据逗号拆分,结果为ID:Name                        
                        json += "[";
                        for (i = 0; i < arr_temp_list.length; i++) {
                            arr_temp_id.push(arr_temp_list[i].substring(0, arr_temp_list[i].indexOf(":")));
                            arr_temp_name.push(arr_temp_list[i].substring(arr_temp_list[i].indexOf(":") + 1, 3000));

                            if (i > 0) {
                                json += ",";
                            }
                            json += "{\"LOT_ID\":\"" + arr_temp_id[i].toString() + "\",\"LOT_NAME\":\"" + escape( arr_temp_name[i].toString()) + "\"}";
                        }
                        json += "]";
                        $("#cmbTempList").combobox("loadData", json);

                    }
                });               

            });

        });       
    </script>

 如果需要将此json绑定给控件 ,则还需要eval:

 json = eval("" + json + "");
$("#cmbtest").combobox("loadData", json);

posted @ 2015-04-08 18:00  无 影  阅读(19370)  评论(0编辑  收藏  举报
作者: Allen【QQ:96966 1314】 网名:无影 出处: http://www.cnblogs.com/allen0118/ 声明: 本文版权归作者和博客园共有!转载时必须保留此段声明,且在文章页面明显位置给出原文连接。