我的递归JSON解析器中的错误在哪里?

时间:2015-05-06 22:29:30

标签: javascript json extjs recursion extjs4

我想创建一个递归函数来解析类似json的数据,如下所示。当key是xtype时,将创建一个新类。特别是,当xtype = gridpanel / treepanel时,所有属性都必须是其构造函数参数,否则,将在创建类之后添加属性。 我的递归函数如下所示,我在ext-all.js的第21行收到错误''太多递归'。

请看一下,我怎么能解决这个问题?

主程序中的

代码:

    me.recursiveParser(null, data.json);
    Ext.apply(me.root, me.parent);
    me.desktopCfg = me.root;

recursiveParser函数:

    recursiveParser: function(nodeName, jsonData) {
             var properties = {};
             var isSpecial = false;
             var isLeaf = true;
             var parent, child, special;

              //Base factor
                    for (var key in jsonData) {
                      var value = jsonData[key];
                      //To collect all the properties that is only initialized with '@'.
                      if (key.toString().indexOf("@") === 0) {
                                key = key.replace("@", "");
                                if(typeof(value) === "string"){
                                     properties[key] = "'"+value+"'";
                                }else{
                                      //Later, should have to deal with the empty value or array with no keys and only elements.
                                      properties[key] = value;
                                }
                                if(key === "xtype"){
                                     //To initialize the root
                                     if(nodeName === null){
                                         this.root = this.createNewObject(value, null);
                                     }

                                     if(value === "gridpanel" || value === "treepanel"){
                                             isSpecial = true;
                                             special = value;
                                     }else{
                                             child = this.createNewObject(value, null);
                                     }
                                }  
                      }else {
                          isLeaf = false;
                      }
                    }

                    if(isSpecial){
                        child = this.createNewObject(special, properties);
                    }

                    //To add the subnode and its properties to its parent object.
                    if (nodeName !== null && typeof(nodeName) === "string") {
                       if(child === null){
                           Ext.apply(parent, properties);

                       }else{
                           Ext.apply(parent, child);
                       }
                    }

                    if(isLeaf){
                        return;
                    }
                    for (var key in jsonData) {
                        var value = jsonData[key];
                        if (key.toString().indexOf("@") === 0) {
                            continue;
                        }else{
                           if(value === "[object Object]"){
                               for(var index in value){
                                   this.recursiveParser(key, value[index]);                                
                               }
                           }else{                                   
                               this.recursiveParser(key, value);
                           }
                           Ext.apply(this.root, parent);
                        }
                    }
}

createNewObject函数:

createNewObject: function(objType, properties){
    if(objType){
                    switch (objType){
                        case "gridpanel":
                            return new MyProg.base.GridPanel(properties);
                            break;

                        case "treepanel":
                            return new MyProg.base.TreePanel(properties);
                            break;

                        case "tabpanel":
                            return new MyProg.base.TabPanel();

                            break;

                        case "tab":
                            return new MyProg.base.Tabs();
                            break;

                        case "formpanel":
                            return new MyProg.base.Accordion(); 
                            break;

                        case "fieldset":
                            return new MyProg.base.FieldSet(); 
                            break;

                        case "textfield":
                            return new MyProg.base.Fields(); 
                            break;

                        case "panel":
                            return new MyProg.base.Accordion(); 
                            break;

                        default:
                            return new MyProg.base.Accordion(); 

                    };
    };
}

data.json:

var data = {
            "json": {
                        "@title": "BusinessIntelligence",
                        "@xtype": "tab",
                        "@layout": "accordion",
                        "items": [
                            {
                                "@title": "SalesReport",
                                "@ctitle": "SalesReport",
                                "@layout": "column",
                                "items": [
                                    {}
                                ]
                            },
                            {
                                "@title": "ContentPlayingReport",
                                "@ctitle": "ContentPlayingReport",
                                "@layout": "column",
                                "items": [
                                    {}
                                ]
                            },
                            {
                                "@title": "BusinessIntelligence",
                                "@ctitle": "BusinessIntelligence",
                                "@layout": "column",
                                "items": [
                                    {}
                                ]
                            }
                        ]
                    }
}

1 个答案:

答案 0 :(得分:0)

我修改了递归部分,现在看起来更优雅。所有的xtype工作得很好,除了gridpanel,我检查DOM,一切都在那里,但仍然收到错误信息:

TypeError: c is undefined
...+g.extraBaseCls);delete g.autoScroll;if(!g.hasView){if(c.buffered&&!c.remoteSort...  
ext-all.js (line 21, col 1184416)

我怀疑这是一个ExtJS错误。我会试着找到另一条出路。

递归计划:

recursiveParser: function (jsonData) {
    var me = this;
    var properties = {};

    for ( var key in jsonData ){
        var value = jsonData[key];
        var items = (value.constructor === Array) ? [] : {};
        if (value instanceof Object) {
            if (isNaN(key)){
                if (items.constructor === Array) {
                    for (var node in value){
                        items.push(me.recursiveParser(value[node]));
                    }
                    properties[key] = items;

                } else {
                    properties[key] = me.recursiveParser(value);
                }
            } else {
                return me.recursiveParser(value);
            }
        } else {
                if (key.toString().indexOf('@') === 0){
                    key = key.replace('@', '');
                    properties[key] = value;
                }                    
        }
    }
    return properties;
}