js json与字符串互转

 字符串转json ,Ext.decode

var data="{"id":"754975cdc482479b9ba2dff9c52a6b47","data":"","nd":"2013","initunitname":"国 家 统 计 局3","initunitcode":"","num0":"23",time":"2014-03-22 11:58:57","_openTime":1395471733216}
";
var  objData = Ext.decode(data);  
 

 json转字符串,Ext.encode

var json={"id":"754975cdc482479b9ba2dff9c52a6b47","data":"","nd":"2013","initunitname":"国 家 统 计 局3","initunitcode":"","num0":"23",time":"2014-03-22 11:58:57","_openTime":1395471733216}
var str=Ext.encode(json);

 将整个json赋值给表单

 setFormValues: function(values, resetDirty) {
            var ownerView = this.ownerView;
            var me = this;
            var form = me.getForm();
            var formId = form.id; //me.formComponentId;
            var _multipletypeValue = "";
            for (var o in values) {
                $("form[id='" + formId + "'] input[name='" + o + "']").each(function() {
                    if ($(this).attr('type') == 'text' || $(this).attr('type') == 'hidden') {
                        $(this).attr('value', values[o]);
                    }
                    else if ($(this).attr('type') == 'radio' & $(this).attr('value') == values[o]) {
                        $(this).attr('checked', "checked");
                    }
                    else if ($(this).attr('type') == 'checkbox' & $(this).attr('value') == values[o]) {
                        $(this).attr('checked', "checked");
                    }
                });
                $("form[id='" + formId + "'] textarea[name='" + o + "']").each(function() {
                    $(this).attr('value', values[o]);
                });
                if (values[o]) {
                    $("form[id='" + formId + "'] select[name='" + o + "']").each(function() {
                        $(this).attr('value', values[o]);
                    });
                }
                //只读状态时各个组件赋值
                $("form[id='" + formId + "'] span[name='" + o + "']").each(function() {
                    var formReadOnly = $(this).attr("formReadOnly");
                    if (me.editmode == "0" || (formReadOnly && formReadOnly == 'y')) {
                        if ($(this).attr("sType") != "codeeditor") {
                            var v = values[o];
                            if (Ext.isArray(v) || Ext.isObject(v)) {
                                v = "";
                            }
                            var localData = $(this).attr("localData");
                            if ($(this).attr("sType") == "multipletype" && localData) {
                                var excfun = $(this).attr('prefunctions');
                                if (excfun) { //调用预处理函数
                                    preExecuteFunction(me, values, $(this), "0");
                                }
                                else { //没有预处理函数
                                    if ($(this).attr("type") != "checkbox") {
                                        Ext.each(localData.split(","), function(item) {
                                            var tmp = item.split("|");
                                            if (tmp[0] == v) {
                                                v = tmp[1];
                                                return false;
                                            }
                                        });
                                    }
                                    else {
                                        var _av = v.split(",");
                                        if (!Ext.isArray(_av)) _av = [_av];
                                        v = $.map(_av, function(v) {
                                            var resv = "";
                                            Ext.each(localData.split(","), function(item) {
                                                var tmp = item.split("|");
                                                if (tmp[0] == v) {
                                                    resv = tmp[1];
                                                    return false;
                                                }
                                            });
                                            return resv;
                                        }).join(",");

                                    }
                                    if (Ext.isString(v)) this.innerHTML = v.replace(/\n/g, "<br/>").replace(/\s/g, "&nbsp;");
                                    else this.innerHTML = v;

                                }
                            }
                            else {
                                if (Ext.isString(v)) this.innerHTML = v.replace(/\n/g, "<br/>").replace(/\s/g, "&nbsp;");
                                else this.innerHTML = v;
                            }
                        }
                    }
                });
                //编辑状态高度自适应textinput组件赋值
                $("form[id='" + formId + "'] span[id='" + o + "_span'][contentEditable]").each(function() {
                    if ($(this).attr("sType") != "codeeditor") {
                        var v = values[o];
                        if (Ext.isArray(v) || Ext.isObject(v)) {
                            v = "";
                        }
                        if (Ext.isNumber(v)) v = v + "";
                        if (v) this.innerHTML = v.replace(/\n/g, "<br/>").replace(/\s/g, "&nbsp;");
                        //$(this).text(v.replace(/\n/g,"<br/>"));
                    }
                });

            }
            //预处理函数执行
            $("form[id='" + formId + "'] *[sType='codeeditor']").each(function(i) {
                preExecuteFunction(me, values, $(this), me.editmode);
            });
            $("form[id='" + formId + "'] *[sType='textinput']").each(function(i) {
                if (me.editmode) { //"0" "1"
                    preExecuteFunction(me, values, $(this), me.editmode);
                }
                else {
                    var formReadOnly = $(this).attr("formReadOnly");
                    if (formReadOnly == "y") //只读
                    preExecuteFunction(me, values, $(this), "0");
                    else preExecuteFunction(me, values, $(this), "1");
                }

            });
            if (resetDirty) {
                me._values = me.rendered ? me.getFormValues() : Ext.applyIf(values, me._originalFormData);
            }
            me.updateComputedText(50);
        }

猜你喜欢

转载自fuanyu.iteye.com/blog/2034910