递归JSON解析器中的错误在哪里? [英] Where is the bug in my recursive JSON parser?

查看:200
本文介绍了递归JSON解析器中的错误在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to create a recursive function to parse json-like data as below. When key is xtype, a new class will be created. In particular, when xtype = gridpanel/treepanel, all the properties have to be its constructor argument, otherwise, properties will be added after class has been created. My recursive function as below, I got an error 'too much recursion' at line 21 in ext-all.js.

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

Please take a look, how am I able to solve this problem?

主程序中的代码: / p>

codes in main program:

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

recursiveParser函数:

recursiveParser function:

    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:

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:

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": [
                                    {}
                                ]
                            }
                        ]
                    }
}


推荐答案

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

I modified the recursion part, it looks more elegant now. All the xtype works just fine, except gridpanel, I've check DOM, everything is in there, but still got error message:

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错误

I suspect it's an ExtJS bug. I'll try to find another way out.

递归程序:

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;
}

这篇关于递归JSON解析器中的错误在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆