gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:57:50 25 4
gpt4 key购买 nike

我正在尝试使用 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]);
}
}

希望这个问题有意义。

最佳答案

这是一个退化的重现案例:

iterateJsonObj("Some text");

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

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

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

关于javascript - 在递归循环中迭代和处理 JSON 对象会导致堆栈超出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15151671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com