我想要做的是递归循环遍历下面JSON中的“注释”,并且每次找到不是对象的东西时,现在只是一个警报或者console.log会做,这两个数据一次与两个对象属性相关联,而不是循环遍历递归循环并分别获取每个部分。我可以这样做,但我想做的是立即获取两个数据,然后使用NEXT obj(如果存在)调用递归循环。这意味着我不知何故需要跟踪下一个obj和/或父obj,这就是我遇到麻烦的地方。我认为也许以某种方式发送next_obj_key
将是要走的路,但我无法弄清楚如何获取该信息以便将其注释掉。
我已经有代码只用注释来调用函数。
以下是目前的代码:
function loopIt(x, next_obj_key) {
//next_obj_key = next_obj_key || 0;
if (typeof x == "object") {
last_obj = x;
for (var attr in x) {
loopIt(x[attr]);
}
} else {
// Not an object, ignore
alert("last_obj[0]: " + last_obj[0] + " --> last_obj[1]: " + last_obj[1] + " --> last_obj[MAX_ROWS]: " + last_obj['MAX_ROWS']);
/*for (var attr in last_obj) {
// Recursive call
alert(last_obj[attr]);
}*/
}
return blah;
}
...这是我正在使用的JSON:
{
"Success": [{
"0": [{
"customer_name": "DOE, JOHN",
"company_name": "CLIENT COMPANY NAME HERE",
"zip": "00000",
"city": "NEW YORK",
"state": "NEW YORK",
"job_number": "XXXXXXXX",
"skus": [{
"0": "SKU 1",
"1": "SKU 2"
}],
"comments": [{
"0": [{
"0": [{
"0": [{
"0": [{
"0": "text",
"1": "11/01/2008 : "
}],
"1": [{
"0": "label",
"1": "Completion date"
}]
}],
"1": [{
"0": [{
"0": "label",
"1": "Note"
}],
"1": [{
"0": "text",
"1": "DELIVERED MERCHANDISE"
}]
}]
}],
"MAX_ROWS": "2",
"MAX_COLS": "2"
}],
"1": [{
"0": [{
"0": [{
"0": [{
"0": "label",
"1": "Dispatched To:"
}],
"1": [{
"0": "text",
"1": "BOB SMITH"
}]
}]
}],
"MAX_ROWS": "1",
"MAX_COLS": "2"
}],
"2": [{
"0": [{
"0": [{
"0": [{
"0": "label",
"1": "Additional instructions"
}],
"1": [{
"0": "text",
"1": "CLIENT REQUEST A CALL"
}]
}]
}],
"MAX_ROWS": "1",
"MAX_COLS": "2"
}],
"3": [{
"0": [{
"0": [{
"0": [{
"0": "",
"1": ""
}],
"1": [{
"0": "text",
"1": "FROM"
}],
"2": [{
"0": "text",
"1": "TO"
}]
}],
"1": [{
"0": [{
"0": "label",
"1": "area_code"
}],
"1": [{
"0": "text",
"1": "xxx"
}],
"2": [{
"0": "text",
"1": "xxx"
}]
}],
"2": [{
"0": [{
"0": "",
"1": ""
}],
"1": [{
"0": "text",
"1": "FROM"
}],
"2": [{
"0": "text",
"1": "TO"
}]
}],
"3": [{
"0": [{
"0": "label",
"1": "value1"
}],
"1": [{
"0": "text",
"1": "xxx"
}],
"2": [{
"0": "text",
"1": "xxx"
}]
}],
"4": [{
"0": [{
"0": "",
"1": ""
}],
"1": [{
"0": "text",
"1": "FROM"
}],
"2": [{
"0": "text",
"1": "TO"
}]
}],
"5": [{
"0": [{
"0": "label",
"1": "value2"
}],
"1": [{
"0": "text",
"1": "xxxx"
}],
"2": [{
"0": "text",
"1": "xxxx"
}]
}]
}],
"MAX_ROWS": "6",
"MAX_COLS": "3"
}],
"4": [{
"0": [{
"0": [{
"0": [{
"0": "label",
"1": "Order Booked"
}],
"1": [{
"0": "text",
"1": [
{}
]
}]
}],
"1": [{
"0": [{
"0": "label",
"1": "Tech Name"
}],
"1": [{
"0": "text",
"1": "TEST TECH NAME"
}]
}],
"2": [{
"0": [{
"0": "label",
"1": "Installer"
}],
"1": [{
"0": "text",
"1": "TERRITORY HERE"
}]
}],
"3": [{
"0": [{
"0": "label",
"1": "Install Date"
}],
"1": [{
"0": "text",
"1": "11/01/2008 AM"
}]
}],
"4": [{
"0": [{
"0": "label",
"1": "Start Time"
}],
"1": [{
"0": "text",
"1": "08:00 AM"
}]
}],
"5": [{
"0": [{
"0": "label",
"1": "End Time"
}],
"1": [{
"0": "text",
"1": "12:00 PM"
}]
}]
}],
"MAX_ROWS": "6",
"MAX_COLS": "2"
}],
"5": [{
"0": [{
"0": [{
"0": [{
"0": "label",
"1": "Source"
}],
"1": [{
"0": "text",
"1": "External Client Tool"
}]
}]
}],
"MAX_ROWS": "1",
"MAX_COLS": "2"
}]
}]
}]
}]
}
答案 0 :(得分:0)
我只想在loopit中更改对象
有点:
function loopIt(x,parent)
{
//next_obj_key = next_obj_key || 0;
if (typeof x == "object")
{
x.parent = parent;
last_obj = x;
for (var attr in x)
{
loopIt(x[attr],x);
}
}
else
{
// Not an object, ignore
alert("last_obj[0]: " + last_obj[0] + " --> last_obj[1]: " + last_obj[1] + " --> last_obj[MAX_ROWS]: " + last_obj['MAX_ROWS'] );
/*for (var attr in last_obj)
{
// Recursive call
alert(last_obj[attr]);
}*/
}
return blah;
}
执行x.parent步骤..您可以直接检查父母,直到它为空...这样您就会知道自己已经达到了食物链的顶端。
是你寻求的吗?