【发布时间】:2012-07-21 21:51:06
【问题描述】:
我正在尝试从 JSON 对象构建一组 HTML 元素。我已经成功地从 HTML 元素构造了对象,但是递归重建对我来说一直失败。谁有好的解决方案?
我的 JSON:
{
"0": {
"id": "text-1",
"tag": "div",
"style": {
"left": "92px",
"top": "37px",
"z-index": "3",
"height": "19px",
"width": "98px",
"font-weight": "bold",
"font-style": "italic",
"font-size": "16px",
"color": "rgb(255, 255, 255)"
},
"data": {},
"children": {
"0": {
"tag": "span",
"style": {},
"data": {},
"html": "This is a test.",
"text": "This is a test."
}
}
},
"1": {
"id": "image-1",
"tag": "div",
"style": {
"width": "100px",
"height": "133px",
"left": "91px",
"top": "8px",
"z-index": "1"
},
"data": {},
"children": {
"0": {
"tag": "img",
"style": {},
"data": {},
"html": "",
"text": "",
"src": "http://img2.etsystatic.com/000/0/6490841/il_570xN.351801334.jpg"
}
}
},
"2": {
"id": "video-1",
"tag": "div",
"style": {
"width": "100px",
"height": "50px",
"left": "5px",
"top": "85px",
"z-index": "2"
},
"data": {},
"children": {
"0": {
"tag": "a",
"style": {
"background-image": "url(http://placehold.it/100x50&text=Video)",
"height": "100%",
"width": "100%",
"display": "block",
"background-position": "0% 0%",
"background-repeat": "no-repeat no-repeat"
},
"data": {},
"html": "",
"text": ""
}
}
}
}
【问题讨论】:
-
嗯...您还没有显示您的实际递归代码,所以我们无法回答您。最重要的是,您已经设法以一种比使用比 XML (JSON) 更简洁而闻名的序列化格式简单地序列化 XML 本身更加臃肿的方式重新创建 XML。如果这是来自 AJAX 请求,为什么不直接使用
responseXML参数,获取根节点,然后将其附加到您想要的 HTML 文档中? -
你应该真正使用数组而不是整数属性对象...
-
那个花哨的“html”属性是什么?
-
我认为传递一个对象数组会更自然,而不是将每个标签分配给具有整数编号索引的属性
-
请贴出一些递归代码。
标签: javascript jquery json recursion