在递归循环中迭代和处理JSON对象会导致堆栈超出错误

时间:2013-03-01 06:13:10

标签: javascript json stack-size

我正在尝试使用JS构建HTML页面。需要进入HTML的细节是从服务器的json对象发送的。现在,json对象的结构基本上模仿了dom结构,我遍历对象并从中获取单个html元素数据并呈现HTML字符串。当我使用递归函数来运行此对象时会发生此问题。我触发了一个Stack Exceeded Error。我想这是因为浏览器堆栈大小的限制。我想了解一下,我可以通过迭代这个对象来创建页面而不会导致脚本失败的最佳方法。

pageObj Structure ->

//only a representation of object, the size is much larger.

{ "Default" : { "#text" : [ "\n  ",
          "\n"
        ],
      "MainForm" : { "#text" : [ "\n    ",
              "\n    ",
              "\n  "
            ],
          "shippingInfo" : { "#text" : [ "\n      ",
                  "\n      ",
                  "\n      ",
                  "\n      ",
                  "\n      ",
                  "\n      ",
                  "\n      ",
                  "\n    "
                ],
              "@attributes" : { "title" : "Shipping Information",
                  "type" : "FormBlock"
                },
              "Row" : [ { "#text" : [ "\n        ",
                        "\n      "
                      ],
                    "fName" : { "@attributes" : { "placeHolder" : "Enter First Name",
                            "title" : "First Name",
                            "type" : "text"
                          } }
                  },
                  { "#text" : [ "\n        ",
                        "\n      "
                      ],
                    "lName" : { "@attributes" : { "placeHolder" : "Enter Last Name",
                            "title" : "Last Name",
                            "type" : "text"
                          } }
                  },
                  { "#text" : [ "\n        ",
                        "\n      "
                      ],
                    "addr1" : { "@attributes" : { "title" : "Address 1",
                            "type" : "text"
                          } }
                  },
                  { "#text" : [ "\n        ",
                        "\n      "
                      ],
                    "addr2" : { "@attributes" : { "title" : "Address 2",
                            "type" : "text"
                          } }
                  },
                  { "#text" : [ "\n        ",
                        "\n        ",
                        "\n      "
                      ],
                    "state" : { "@attributes" : { "title" : "State",
                            "type" : "text"
                          } },
                    "zipCode" : { "@attributes" : { "title" : "Zip Code",
                            "type" : "text"
                          } }
                  },
                  { "#text" : [ "\n        ",
                        "\n        ",
                        "\n      "
                      ],
                    "country" : { "@attributes" : { "title" : "Country",
                            "type" : "text"
                          } },
                    "phone" : { "@attributes" : { "title" : "Phone",
                            "type" : "text"
                          } }
                  },
                  { "#text" : [ "\n        ",
                        "\n        ",
                        "\n        ",
                        "\n        ",
                        "\n      "
                      ],
                    "day10" : { "@attributes" : { "title" : "10 day Shipping ($3)",
                            "type" : "radio"
                          } },
                    "day5" : { "@attributes" : { "title" : "5 Shipping ($10)",
                            "type" : "radio"
                          } },
                    "free" : { "@attributes" : { "title" : "Free Shipping ($0)",
                            "type" : "radio"
                          } },
                    "overNight" : { "@attributes" : { "title" : "One day Shipping ($20)",
                            "type" : "radio"
                          } }
                  }
                ]
            },
          "userInfo" : { "#text" : [ "\n      ",
                  "\n      ",
                  "\n      ",
                  "\n    "
                ],
              "@attributes" : { "title" : "User Information",
                  "type" : "FormBlock"
                },
              "Row" : [ { "#text" : [ "\n        ",
                        "\n      "
                      ],
                    "TextBox" : { "@attributes" : { "placeHolder" : "Select an username",
                            "title" : "Username",
                            "type" : "text"
                          } }
                  },
                  { "#text" : [ "\n        ",
                        "\n      "
                      ],
                    "TextBox" : { "@attributes" : { "placeHolder" : "Select a password",
                            "title" : "Password",
                            "type" : "password"
                          } }
                  },
                  { "#text" : [ "\n        ",
                        "\n      "
                      ],
                    "TextBox" : { "@attributes" : { "placeHolder" : "Eg: name@gmail.com",
                            "title" : "Email",
                            "type" : "text"
                          } }
                  }
                ]
            }
        }
    } }

要迭代这个对象,我使用下面的技术。

function iterateJsonObj(obj) {
    for(key in obj) {
        if(!obj.hasOwnProperty(key) || key=="#text") {
            continue;
        }
        else if(obj[key]["@attributes"]!=null)
        {
            htmlStr += createHTMLStr(obj[key], key);
        }

        iterateJsonObj(obj[key]);
    }
}

希望这个问题有道理。

1 个答案:

答案 0 :(得分:2)

这是一个堕落的责备案例:

iterateJsonObj("Some text");

你能看到发生了什么吗? for循环显然类似于单字符子串的数组处理字符串。 “Shipping”[0]是“S”,它本身就是一个字符串......

要解决这个问题,我建议在以这种方式迭代它之前测试obof [key] ===“object”的类型。

另外,写单元测试。它们会让你的生活更轻松。 :)