0

我正在尝试使用 JS 构建 HTML 页面。需要进入 HTML 的内容的详细信息从服务器以 json 对象的形式发送。现在,json 对象的结构基本上模仿了 dom 结构,我遍历对象并从中获取单个 html 元素数据并呈现 HTML 字符串。当我使用递归函数运行此对象时,就会出现问题。我触发了堆栈超出错误。我猜这是因为浏览器堆栈大小的限制。我想了解,在不导致脚本失败的情况下,我可以迭代此对象以创建页面的最佳方式是什么。

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

希望这个问题有意义。

4

1 回答 1

2

这是一个退化的复制案例:

iterateJsonObj("Some text");

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

为了解决这个问题,我建议在以这种方式迭代之前测试 typeof obj[key] === "object"。

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

于 2013-03-01T07:25:30.590 回答