- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在努力寻找解决方案,但没有任何结果。由于限制,必须从专有服务器给我的 JSON 对象构建移动导航系统。他们允许使用 jQuery 1.12.4,但不能使用其他框架,甚至不能使用更新的 jQuery 3。无法加载 PHP、Python 或任何其他语言。因此必须找到纯 javascript 或 jQuery 1.12.4 解决方案。
将有可变数量的菜单项和可变数量的子菜单项。我知道您可以通过括号表示法访问 (不能使用点表示法,因为键中有来自服务器的点) 但是您如何使用变量或在循环中构建它。
示例我正在尝试获取内部对象 L.3.170 但需要可变地构建括号引用。并从字符串中创建“后退”功能。
从字符串“L.1.171~L.2.170”构建以下 REF,最初是在波浪号上拆分并从循环构建 REF,但没有找到任何关于如何做到这一点的示例。我找到了一些 eval 的,但它们似乎不起作用。
REF 构建:OBJ.root['L.1.171']['L.2.170']
要构建的子菜单:OBJ.root['L.1.171']['L.2.170']['L.3.170']
用可变括号替换文字括号,子菜单的深度将可变。
所有“L”键名称都是唯一的,那么是否有某种查找功能(如数组)?没有任何关于如何在 JSON 中查找子对象的计划或工作。
来自服务器的 JSON (OBJ):
{
"root": {
"SubCount": 4,
"L.1.22": {
"img": "",
"CategoryID": 635,
"SubCount": 4,
"L.2.4": {
"img": "",
"CategoryID": 692,
"SubCount": 1,
"L.3.4": { "img": "", "CategoryID": 229, "SubCount": 0 }
},
"L.2.10": {
"img": "",
"CategoryID": 213,
"SubCount": 5,
"L.3.6": { "img": "", "CategoryID": 46, "SubCount": 0 },
"L.3.7": { "img": "", "CategoryID": 48, "SubCount": 0 },
"L.3.8": { "img": "", "CategoryID": 49, "SubCount": 0 },
"L.3.9": { "img": "", "CategoryID": 50, "SubCount": 0 },
"L.3.10": { "img": "", "CategoryID": 95, "SubCount": 0 }
},
"L.2.14": {
"img": "",
"CategoryID": 212,
"SubCount": 3,
"L.3.12": { "img": "", "CategoryID": 53, "SubCount": 0 },
"L.3.13": { "img": "", "CategoryID": 55, "SubCount": 0 },
"L.3.14": { "img": "", "CategoryID": 98, "SubCount": 0 }
},
"L.2.22": {
"img": "",
"CategoryID": 211,
"SubCount": 7,
"L.3.16": { "img": "", "CategoryID": 46, "SubCount": 0 },
"L.3.17": { "img": "", "CategoryID": 49, "SubCount": 0 },
"L.3.18": { "img": "", "CategoryID": 50, "SubCount": 0 },
"L.3.19": { "img": "", "CategoryID": 52, "SubCount": 0 },
"L.3.20": { "img": "", "CategoryID": 54, "SubCount": 0 },
"L.3.21": { "img": "", "CategoryID": 95, "SubCount": 0 },
"L.3.22": { "img": "", "CategoryID": 98, "SubCount": 0 }
}
},
"L.1.100": {
"img": "",
"CategoryID": 689,
"SubCount": 6,
"L.2.38": {
"img": "",
"CategoryID": 225,
"SubCount": 14,
"L.3.25": { "img": "", "CategoryID": 612, "SubCount": 0 },
"L.3.26": { "img": "", "CategoryID": 773, "SubCount": 0 },
"L.3.27": { "img": "", "CategoryID": 1174, "SubCount": 0 },
"L.3.28": { "img": "", "CategoryID": 135, "SubCount": 0 },
"L.3.29": { "img": "", "CategoryID": 451, "SubCount": 0 },
"L.3.30": { "img": "", "CategoryID": 1169, "SubCount": 0 },
"L.3.31": { "img": "", "CategoryID": 403, "SubCount": 0 },
"L.3.32": { "img": "", "CategoryID": 136, "SubCount": 0 },
"L.3.33": { "img": "", "CategoryID": 133, "SubCount": 0 },
"L.3.34": { "img": "", "CategoryID": 134, "SubCount": 0 },
"L.3.35": { "img": "", "CategoryID": 138, "SubCount": 0 },
"L.3.36": { "img": "", "CategoryID": 137, "SubCount": 0 },
"L.3.37": { "img": "", "CategoryID": 132, "SubCount": 0 },
"L.3.38": { "img": "", "CategoryID": 130, "SubCount": 0 }
},
"L.2.53": {
"img": "",
"CategoryID": 217,
"SubCount": 14,
"L.3.40": { "img": "", "CategoryID": 108, "SubCount": 0 },
"L.3.41": { "img": "", "CategoryID": 611, "SubCount": 0 },
"L.3.42": { "img": "", "CategoryID": 772, "SubCount": 0 },
"L.3.43": { "img": "", "CategoryID": 448, "SubCount": 0 },
"L.3.44": { "img": "", "CategoryID": 103, "SubCount": 0 },
"L.3.45": { "img": "", "CategoryID": 404, "SubCount": 0 },
"L.3.46": { "img": "", "CategoryID": 106, "SubCount": 0 },
"L.3.47": { "img": "", "CategoryID": 105, "SubCount": 0 },
"L.3.48": { "img": "", "CategoryID": 107, "SubCount": 0 },
"L.3.49": { "img": "", "CategoryID": 104, "SubCount": 0 },
"L.3.50": { "img": "", "CategoryID": 266, "SubCount": 0 },
"L.3.51": { "img": "", "CategoryID": 1168, "SubCount": 0 },
"L.3.52": { "img": "", "CategoryID": 1173, "SubCount": 0 },
"L.3.53": { "img": "", "CategoryID": 112, "SubCount": 0 }
},
"L.2.68": {
"img": "",
"CategoryID": 219,
"SubCount": 14,
"L.3.55": { "img": "", "CategoryID": 613, "SubCount": 0 },
"L.3.56": { "img": "", "CategoryID": 775, "SubCount": 0 },
"L.3.57": { "img": "", "CategoryID": 450, "SubCount": 0 },
"L.3.58": { "img": "", "CategoryID": 144, "SubCount": 0 },
"L.3.59": { "img": "", "CategoryID": 146, "SubCount": 0 },
"L.3.60": { "img": "", "CategoryID": 400, "SubCount": 0 },
"L.3.61": { "img": "", "CategoryID": 145, "SubCount": 0 },
"L.3.62": { "img": "", "CategoryID": 151, "SubCount": 0 },
"L.3.63": { "img": "", "CategoryID": 150, "SubCount": 0 },
"L.3.64": { "img": "", "CategoryID": 147, "SubCount": 0 },
"L.3.65": { "img": "", "CategoryID": 1170, "SubCount": 0 },
"L.3.66": { "img": "", "CategoryID": 1176, "SubCount": 0 },
"L.3.67": { "img": "", "CategoryID": 143, "SubCount": 0 },
"L.3.68": { "img": "", "CategoryID": 141, "SubCount": 0 }
},
"L.2.83": {
"img": "",
"CategoryID": 220,
"SubCount": 14,
"L.3.70": { "img": "", "CategoryID": 614, "SubCount": 0 },
"L.3.71": { "img": "", "CategoryID": 774, "SubCount": 0 },
"L.3.72": { "img": "", "CategoryID": 1171, "SubCount": 0 },
"L.3.73": { "img": "", "CategoryID": 1175, "SubCount": 0 },
"L.3.74": { "img": "", "CategoryID": 453, "SubCount": 0 },
"L.3.75": { "img": "", "CategoryID": 153, "SubCount": 0 },
"L.3.76": { "img": "", "CategoryID": 399, "SubCount": 0 },
"L.3.77": { "img": "", "CategoryID": 154, "SubCount": 0 },
"L.3.78": { "img": "", "CategoryID": 155, "SubCount": 0 },
"L.3.79": { "img": "", "CategoryID": 161, "SubCount": 0 },
"L.3.80": { "img": "", "CategoryID": 160, "SubCount": 0 },
"L.3.81": { "img": "", "CategoryID": 156, "SubCount": 0 },
"L.3.82": { "img": "", "CategoryID": 159, "SubCount": 0 },
"L.3.83": { "img": "", "CategoryID": 158, "SubCount": 0 }
},
"L.2.98": {
"img": "",
"CategoryID": 698,
"SubCount": 14,
"L.3.85": { "img": "", "CategoryID": 776, "SubCount": 0 },
"L.3.86": { "img": "", "CategoryID": 1167, "SubCount": 0 },
"L.3.87": { "img": "", "CategoryID": 253, "SubCount": 0 },
"L.3.88": { "img": "", "CategoryID": 608, "SubCount": 0 },
"L.3.89": { "img": "", "CategoryID": 473, "SubCount": 0 },
"L.3.90": { "img": "", "CategoryID": 256, "SubCount": 0 },
"L.3.91": { "img": "", "CategoryID": 258, "SubCount": 0 },
"L.3.92": { "img": "", "CategoryID": 259, "SubCount": 0 },
"L.3.93": { "img": "", "CategoryID": 260, "SubCount": 0 },
"L.3.94": { "img": "", "CategoryID": 254, "SubCount": 0 },
"L.3.95": { "img": "", "CategoryID": 255, "SubCount": 0 },
"L.3.96": { "img": "", "CategoryID": 262, "SubCount": 0 },
"L.3.97": { "img": "", "CategoryID": 1172, "SubCount": 0 },
"L.3.98": { "img": "", "CategoryID": 397, "SubCount": 0 }
},
"L.2.100": {
"img": "",
"CategoryID": 218,
"SubCount": 1,
"L.3.100": { "img": "", "CategoryID": 208, "SubCount": 0 }
}
},
"L.1.122": {
"img": "",
"CategoryID": 870,
"SubCount": 6,
"L.2.108": {
"img": "", "CategoryID": 871, "SubCount": 6,
"L.3.103": { "img": "", "CategoryID": 883, "SubCount": 0 },
"L.3.104": { "img": "", "CategoryID": 885, "SubCount": 0 },
"L.3.105": { "img": "", "CategoryID": 873, "SubCount": 0 },
"L.3.106": { "img": "", "CategoryID": 886, "SubCount": 0 },
"L.3.107": { "img": "", "CategoryID": 872, "SubCount": 0 },
"L.3.108": { "img": "", "CategoryID": 884, "SubCount": 0 }
},
"L.2.109": { "img": "", "CategoryID": 886, "SubCount": 0 },
"L.2.110": { "img": "", "CategoryID": 872, "SubCount": 0 },
"L.2.111": { "img": "", "CategoryID": 884, "SubCount": 0 },
"L.2.112": { "img": "", "CategoryID": 876, "SubCount": 0 },
"L.2.122": {
"img": "",
"CategoryID": 672,
"SubCount": 9,
"L.3.114": { "img": "", "CategoryID": 839, "SubCount": 0 },
"L.3.115": { "img": "", "CategoryID": 210, "SubCount": 0 },
"L.3.116": { "img": "", "CategoryID": 277, "SubCount": 0 },
"L.3.117": { "img": "", "CategoryID": 639, "SubCount": 0 },
"L.3.118": { "img": "", "CategoryID": 870, "SubCount": 0 },
"L.3.119": { "img": "", "CategoryID": 282, "SubCount": 0 },
"L.3.120": { "img": "", "CategoryID": 1191, "SubCount": 0 },
"L.3.121": { "img": "", "CategoryID": 932, "SubCount": 0 },
"L.3.122": { "img": "", "CategoryID": 332, "SubCount": 0 }
}
},
"L.1.171": {
"img": "",
"CategoryID": 672,
"SubCount": 9,
"L.2.124": { "img": "", "CategoryID": 839, "SubCount": 0 },
"L.2.125": { "img": "", "CategoryID": 210, "SubCount": 0 },
"L.2.132": {
"img": "",
"CategoryID": 277,
"SubCount": 6,
"L.3.127": { "img": "1173128.Chat Icon.png", "CategoryID": 1211, "SubCount": 0 },
"L.3.128": { "img": "", "CategoryID": 1159, "SubCount": 0 },
"L.3.129": { "img": "", "CategoryID": 1084, "SubCount": 0 },
"L.3.130": { "img": "", "CategoryID": 1150, "SubCount": 0 },
"L.3.131": { "img": "", "CategoryID": 1138, "SubCount": 0 },
"L.3.132": { "img": "", "CategoryID": 1119, "SubCount": 0 }
},
"L.2.145": {
"img": "",
"CategoryID": 639,
"SubCount": 12,
"L.3.134": { "img": "", "CategoryID": 122, "SubCount": 0 },
"L.3.135": { "img": "", "CategoryID": 121, "SubCount": 0 },
"L.3.136": { "img": "", "CategoryID": 512, "SubCount": 0 },
"L.3.137": { "img": "", "CategoryID": 124, "SubCount": 0 },
"L.3.138": { "img": "", "CategoryID": 125, "SubCount": 0 },
"L.3.139": { "img": "", "CategoryID": 127, "SubCount": 0 },
"L.3.140": { "img": "", "CategoryID": 878, "SubCount": 0 },
"L.3.141": { "img": "", "CategoryID": 877, "SubCount": 0 },
"L.3.142": { "img": "", "CategoryID": 880, "SubCount": 0 },
"L.3.143": { "img": "", "CategoryID": 879, "SubCount": 0 },
"L.3.144": { "img": "", "CategoryID": 874, "SubCount": 0 },
"L.3.145": { "img": "", "CategoryID": 282, "SubCount": 0 }
},
"L.2.152": {
"img": "",
"CategoryID": 870,
"SubCount": 6,
"L.3.147": { "img": "", "CategoryID": 871, "SubCount": 0 },
"L.3.148": { "img": "", "CategoryID": 886, "SubCount": 0 },
"L.3.149": { "img": "", "CategoryID": 872, "SubCount": 0 },
"L.3.150": { "img": "", "CategoryID": 884, "SubCount": 0 },
"L.3.151": { "img": "", "CategoryID": 876, "SubCount": 0 },
"L.3.152": { "img": "", "CategoryID": 672, "SubCount": 0 }
},
"L.2.158": {
"img": "",
"CategoryID": 282,
"SubCount": 5,
"L.3.154": { "img": "", "CategoryID": 122, "SubCount": 0 },
"L.3.155": { "img": "", "CategoryID": 512, "SubCount": 0 },
"L.3.156": { "img": "", "CategoryID": 121, "SubCount": 0 },
"L.3.157": { "img": "", "CategoryID": 124, "SubCount": 0 },
"L.3.158": { "img": "", "CategoryID": 127, "SubCount": 0 }
},
"L.2.165": {
"img": "",
"CategoryID": 1191,
"SubCount": 6,
"L.3.160": { "img": "", "CategoryID": 1184, "SubCount": 0 },
"L.3.161": { "img": "", "CategoryID": 1186, "SubCount": 0 },
"L.3.162": { "img": "", "CategoryID": 1185, "SubCount": 0 },
"L.3.163": { "img": "", "CategoryID": 1187, "SubCount": 0 },
"L.3.164": { "img": "", "CategoryID": 1189, "SubCount": 0 },
"L.3.165": { "img": "", "CategoryID": 1190, "SubCount": 0 }
},
"L.2.170": {
"img": "",
"CategoryID": 932,
"SubCount": 4,
"L.3.167": { "img": "", "CategoryID": 936, "SubCount": 0 },
"L.3.168": { "img": "", "CategoryID": 1181, "SubCount": 0 },
"L.3.169": { "img": "", "CategoryID": 331, "SubCount": 0 },
"L.3.170": { "img": "", "CategoryID": 1182, "SubCount": 0 }
},
"L.2.171": { "img": "", "CategoryID": 332, "SubCount": 0 }
}
}
}
最佳答案
我认为,如果您更明确地说明自己最终想要什么,您可能会得到更好的答案。我会尝试填写一些我认为您正在寻找的内容。
如果你有一个像 "L.1.171~L.2.170"
这样的字符串,你可以简单地拆分和减少以得到对象,比如:
var OBJ = { "root": { "SubCount": 4, "L.1.22": { "img": "", "CategoryID": 635, "SubCount": 4, "L.2.4": { "img": "", "CategoryID": 692, "SubCount": 1, "L.3.4": { "img": "", "CategoryID": 229, "SubCount": 0 } }, "L.2.10": { "img": "", "CategoryID": 213, "SubCount": 5, "L.3.6": { "img": "", "CategoryID": 46, "SubCount": 0 }, "L.3.7": { "img": "", "CategoryID": 48, "SubCount": 0 }, "L.3.8": { "img": "", "CategoryID": 49, "SubCount": 0 }, "L.3.9": { "img": "", "CategoryID": 50, "SubCount": 0 }, "L.3.10": { "img": "", "CategoryID": 95, "SubCount": 0 } }, "L.2.14": { "img": "", "CategoryID": 212, "SubCount": 3, "L.3.12": { "img": "", "CategoryID": 53, "SubCount": 0 }, "L.3.13": { "img": "", "CategoryID": 55, "SubCount": 0 }, "L.3.14": { "img": "", "CategoryID": 98, "SubCount": 0 } }, "L.2.22": { "img": "", "CategoryID": 211, "SubCount": 7, "L.3.16": { "img": "", "CategoryID": 46, "SubCount": 0 }, "L.3.17": { "img": "", "CategoryID": 49, "SubCount": 0 }, "L.3.18": { "img": "", "CategoryID": 50, "SubCount": 0 }, "L.3.19": { "img": "", "CategoryID": 52, "SubCount": 0 }, "L.3.20": { "img": "", "CategoryID": 54, "SubCount": 0 }, "L.3.21": { "img": "", "CategoryID": 95, "SubCount": 0 }, "L.3.22": { "img": "", "CategoryID": 98, "SubCount": 0 } } }, "L.1.100": { "img": "", "CategoryID": 689, "SubCount": 6, "L.2.38": { "img": "", "CategoryID": 225, "SubCount": 14, "L.3.25": { "img": "", "CategoryID": 612, "SubCount": 0 }, "L.3.26": { "img": "", "CategoryID": 773, "SubCount": 0 }, "L.3.27": { "img": "", "CategoryID": 1174, "SubCount": 0 }, "L.3.28": { "img": "", "CategoryID": 135, "SubCount": 0 }, "L.3.29": { "img": "", "CategoryID": 451, "SubCount": 0 }, "L.3.30": { "img": "", "CategoryID": 1169, "SubCount": 0 }, "L.3.31": { "img": "", "CategoryID": 403, "SubCount": 0 }, "L.3.32": { "img": "", "CategoryID": 136, "SubCount": 0 }, "L.3.33": { "img": "", "CategoryID": 133, "SubCount": 0 }, "L.3.34": { "img": "", "CategoryID": 134, "SubCount": 0 }, "L.3.35": { "img": "", "CategoryID": 138, "SubCount": 0 }, "L.3.36": { "img": "", "CategoryID": 137, "SubCount": 0 }, "L.3.37": { "img": "", "CategoryID": 132, "SubCount": 0 }, "L.3.38": { "img": "", "CategoryID": 130, "SubCount": 0 } }, "L.2.53": { "img": "", "CategoryID": 217, "SubCount": 14, "L.3.40": { "img": "", "CategoryID": 108, "SubCount": 0 }, "L.3.41": { "img": "", "CategoryID": 611, "SubCount": 0 }, "L.3.42": { "img": "", "CategoryID": 772, "SubCount": 0 }, "L.3.43": { "img": "", "CategoryID": 448, "SubCount": 0 }, "L.3.44": { "img": "", "CategoryID": 103, "SubCount": 0 }, "L.3.45": { "img": "", "CategoryID": 404, "SubCount": 0 }, "L.3.46": { "img": "", "CategoryID": 106, "SubCount": 0 }, "L.3.47": { "img": "", "CategoryID": 105, "SubCount": 0 }, "L.3.48": { "img": "", "CategoryID": 107, "SubCount": 0 }, "L.3.49": { "img": "", "CategoryID": 104, "SubCount": 0 }, "L.3.50": { "img": "", "CategoryID": 266, "SubCount": 0 }, "L.3.51": { "img": "", "CategoryID": 1168, "SubCount": 0 }, "L.3.52": { "img": "", "CategoryID": 1173, "SubCount": 0 }, "L.3.53": { "img": "", "CategoryID": 112, "SubCount": 0 } }, "L.2.68": { "img": "", "CategoryID": 219, "SubCount": 14, "L.3.55": { "img": "", "CategoryID": 613, "SubCount": 0 }, "L.3.56": { "img": "", "CategoryID": 775, "SubCount": 0 }, "L.3.57": { "img": "", "CategoryID": 450, "SubCount": 0 }, "L.3.58": { "img": "", "CategoryID": 144, "SubCount": 0 }, "L.3.59": { "img": "", "CategoryID": 146, "SubCount": 0 }, "L.3.60": { "img": "", "CategoryID": 400, "SubCount": 0 }, "L.3.61": { "img": "", "CategoryID": 145, "SubCount": 0 }, "L.3.62": { "img": "", "CategoryID": 151, "SubCount": 0 }, "L.3.63": { "img": "", "CategoryID": 150, "SubCount": 0 }, "L.3.64": { "img": "", "CategoryID": 147, "SubCount": 0 }, "L.3.65": { "img": "", "CategoryID": 1170, "SubCount": 0 }, "L.3.66": { "img": "", "CategoryID": 1176, "SubCount": 0 }, "L.3.67": { "img": "", "CategoryID": 143, "SubCount": 0 }, "L.3.68": { "img": "", "CategoryID": 141, "SubCount": 0 } }, "L.2.83": { "img": "", "CategoryID": 220, "SubCount": 14, "L.3.70": { "img": "", "CategoryID": 614, "SubCount": 0 }, "L.3.71": { "img": "", "CategoryID": 774, "SubCount": 0 }, "L.3.72": { "img": "", "CategoryID": 1171, "SubCount": 0 }, "L.3.73": { "img": "", "CategoryID": 1175, "SubCount": 0 }, "L.3.74": { "img": "", "CategoryID": 453, "SubCount": 0 }, "L.3.75": { "img": "", "CategoryID": 153, "SubCount": 0 }, "L.3.76": { "img": "", "CategoryID": 399, "SubCount": 0 }, "L.3.77": { "img": "", "CategoryID": 154, "SubCount": 0 }, "L.3.78": { "img": "", "CategoryID": 155, "SubCount": 0 }, "L.3.79": { "img": "", "CategoryID": 161, "SubCount": 0 }, "L.3.80": { "img": "", "CategoryID": 160, "SubCount": 0 }, "L.3.81": { "img": "", "CategoryID": 156, "SubCount": 0 }, "L.3.82": { "img": "", "CategoryID": 159, "SubCount": 0 }, "L.3.83": { "img": "", "CategoryID": 158, "SubCount": 0 } }, "L.2.98": { "img": "", "CategoryID": 698, "SubCount": 14, "L.3.85": { "img": "", "CategoryID": 776, "SubCount": 0 }, "L.3.86": { "img": "", "CategoryID": 1167, "SubCount": 0 }, "L.3.87": { "img": "", "CategoryID": 253, "SubCount": 0 }, "L.3.88": { "img": "", "CategoryID": 608, "SubCount": 0 }, "L.3.89": { "img": "", "CategoryID": 473, "SubCount": 0 }, "L.3.90": { "img": "", "CategoryID": 256, "SubCount": 0 }, "L.3.91": { "img": "", "CategoryID": 258, "SubCount": 0 }, "L.3.92": { "img": "", "CategoryID": 259, "SubCount": 0 }, "L.3.93": { "img": "", "CategoryID": 260, "SubCount": 0 }, "L.3.94": { "img": "", "CategoryID": 254, "SubCount": 0 }, "L.3.95": { "img": "", "CategoryID": 255, "SubCount": 0 }, "L.3.96": { "img": "", "CategoryID": 262, "SubCount": 0 }, "L.3.97": { "img": "", "CategoryID": 1172, "SubCount": 0 }, "L.3.98": { "img": "", "CategoryID": 397, "SubCount": 0 } }, "L.2.100": { "img": "", "CategoryID": 218, "SubCount": 1, "L.3.100": { "img": "", "CategoryID": 208, "SubCount": 0 } } }, "L.1.122": { "img": "", "CategoryID": 870, "SubCount": 6, "L.2.108": { "img": "", "CategoryID": 871, "SubCount": 6, "L.3.103": { "img": "", "CategoryID": 883, "SubCount": 0 }, "L.3.104": { "img": "", "CategoryID": 885, "SubCount": 0 }, "L.3.105": { "img": "", "CategoryID": 873, "SubCount": 0 }, "L.3.106": { "img": "", "CategoryID": 886, "SubCount": 0 }, "L.3.107": { "img": "", "CategoryID": 872, "SubCount": 0 }, "L.3.108": { "img": "", "CategoryID": 884, "SubCount": 0 } }, "L.2.109": { "img": "", "CategoryID": 886, "SubCount": 0 }, "L.2.110": { "img": "", "CategoryID": 872, "SubCount": 0 }, "L.2.111": { "img": "", "CategoryID": 884, "SubCount": 0 }, "L.2.112": { "img": "", "CategoryID": 876, "SubCount": 0 }, "L.2.122": { "img": "", "CategoryID": 672, "SubCount": 9, "L.3.114": { "img": "", "CategoryID": 839, "SubCount": 0 }, "L.3.115": { "img": "", "CategoryID": 210, "SubCount": 0 }, "L.3.116": { "img": "", "CategoryID": 277, "SubCount": 0 }, "L.3.117": { "img": "", "CategoryID": 639, "SubCount": 0 }, "L.3.118": { "img": "", "CategoryID": 870, "SubCount": 0 }, "L.3.119": { "img": "", "CategoryID": 282, "SubCount": 0 }, "L.3.120": { "img": "", "CategoryID": 1191, "SubCount": 0 }, "L.3.121": { "img": "", "CategoryID": 932, "SubCount": 0 }, "L.3.122": { "img": "", "CategoryID": 332, "SubCount": 0 } } }, "L.1.171": { "img": "", "CategoryID": 672, "SubCount": 9, "L.2.124": { "img": "", "CategoryID": 839, "SubCount": 0 }, "L.2.125": { "img": "", "CategoryID": 210, "SubCount": 0 }, "L.2.132": { "img": "", "CategoryID": 277, "SubCount": 6, "L.3.127": { "img": "1173128.Chat Icon.png", "CategoryID": 1211, "SubCount": 0 }, "L.3.128": { "img": "", "CategoryID": 1159, "SubCount": 0 }, "L.3.129": { "img": "", "CategoryID": 1084, "SubCount": 0 }, "L.3.130": { "img": "", "CategoryID": 1150, "SubCount": 0 }, "L.3.131": { "img": "", "CategoryID": 1138, "SubCount": 0 }, "L.3.132": { "img": "", "CategoryID": 1119, "SubCount": 0 } }, "L.2.145": { "img": "", "CategoryID": 639, "SubCount": 12, "L.3.134": { "img": "", "CategoryID": 122, "SubCount": 0 }, "L.3.135": { "img": "", "CategoryID": 121, "SubCount": 0 }, "L.3.136": { "img": "", "CategoryID": 512, "SubCount": 0 }, "L.3.137": { "img": "", "CategoryID": 124, "SubCount": 0 }, "L.3.138": { "img": "", "CategoryID": 125, "SubCount": 0 }, "L.3.139": { "img": "", "CategoryID": 127, "SubCount": 0 }, "L.3.140": { "img": "", "CategoryID": 878, "SubCount": 0 }, "L.3.141": { "img": "", "CategoryID": 877, "SubCount": 0 }, "L.3.142": { "img": "", "CategoryID": 880, "SubCount": 0 }, "L.3.143": { "img": "", "CategoryID": 879, "SubCount": 0 }, "L.3.144": { "img": "", "CategoryID": 874, "SubCount": 0 }, "L.3.145": { "img": "", "CategoryID": 282, "SubCount": 0 } }, "L.2.152": { "img": "", "CategoryID": 870, "SubCount": 6, "L.3.147": { "img": "", "CategoryID": 871, "SubCount": 0 }, "L.3.148": { "img": "", "CategoryID": 886, "SubCount": 0 }, "L.3.149": { "img": "", "CategoryID": 872, "SubCount": 0 }, "L.3.150": { "img": "", "CategoryID": 884, "SubCount": 0 }, "L.3.151": { "img": "", "CategoryID": 876, "SubCount": 0 }, "L.3.152": { "img": "", "CategoryID": 672, "SubCount": 0 } }, "L.2.158": { "img": "", "CategoryID": 282, "SubCount": 5, "L.3.154": { "img": "", "CategoryID": 122, "SubCount": 0 }, "L.3.155": { "img": "", "CategoryID": 512, "SubCount": 0 }, "L.3.156": { "img": "", "CategoryID": 121, "SubCount": 0 }, "L.3.157": { "img": "", "CategoryID": 124, "SubCount": 0 }, "L.3.158": { "img": "", "CategoryID": 127, "SubCount": 0 } }, "L.2.165": { "img": "", "CategoryID": 1191, "SubCount": 6, "L.3.160": { "img": "", "CategoryID": 1184, "SubCount": 0 }, "L.3.161": { "img": "", "CategoryID": 1186, "SubCount": 0 }, "L.3.162": { "img": "", "CategoryID": 1185, "SubCount": 0 }, "L.3.163": { "img": "", "CategoryID": 1187, "SubCount": 0 }, "L.3.164": { "img": "", "CategoryID": 1189, "SubCount": 0 }, "L.3.165": { "img": "", "CategoryID": 1190, "SubCount": 0 } }, "L.2.170": { "img": "", "CategoryID": 932, "SubCount": 4, "L.3.167": { "img": "", "CategoryID": 936, "SubCount": 0 }, "L.3.168": { "img": "", "CategoryID": 1181, "SubCount": 0 }, "L.3.169": { "img": "", "CategoryID": 331, "SubCount": 0 }, "L.3.170": { "img": "", "CategoryID": 1182, "SubCount": 0 } }, "L.2.171": { "img": "", "CategoryID": 332, "SubCount": 0 } } }}
let key_string = 'L.1.171~L.2.170'
let keys = key_string.split('~')
let found = keys.reduce((a, c) => a[c], OBJ.root)
console.log(found)
可能有很多方法可以找到给定键的特定对象。一种方法是将您的结构扁平化为一个具有单一级别键的对象。然后你可以通过键获得一个特定的对象,例如:
var OBJ = { "root": { "SubCount": 4, "L.1.22": { "img": "", "CategoryID": 635, "SubCount": 4, "L.2.4": { "img": "", "CategoryID": 692, "SubCount": 1, "L.3.4": { "img": "", "CategoryID": 229, "SubCount": 0 } }, "L.2.10": { "img": "", "CategoryID": 213, "SubCount": 5, "L.3.6": { "img": "", "CategoryID": 46, "SubCount": 0 }, "L.3.7": { "img": "", "CategoryID": 48, "SubCount": 0 }, "L.3.8": { "img": "", "CategoryID": 49, "SubCount": 0 }, "L.3.9": { "img": "", "CategoryID": 50, "SubCount": 0 }, "L.3.10": { "img": "", "CategoryID": 95, "SubCount": 0 } }, "L.2.14": { "img": "", "CategoryID": 212, "SubCount": 3, "L.3.12": { "img": "", "CategoryID": 53, "SubCount": 0 }, "L.3.13": { "img": "", "CategoryID": 55, "SubCount": 0 }, "L.3.14": { "img": "", "CategoryID": 98, "SubCount": 0 } }, "L.2.22": { "img": "", "CategoryID": 211, "SubCount": 7, "L.3.16": { "img": "", "CategoryID": 46, "SubCount": 0 }, "L.3.17": { "img": "", "CategoryID": 49, "SubCount": 0 }, "L.3.18": { "img": "", "CategoryID": 50, "SubCount": 0 }, "L.3.19": { "img": "", "CategoryID": 52, "SubCount": 0 }, "L.3.20": { "img": "", "CategoryID": 54, "SubCount": 0 }, "L.3.21": { "img": "", "CategoryID": 95, "SubCount": 0 }, "L.3.22": { "img": "", "CategoryID": 98, "SubCount": 0 } } }, "L.1.100": { "img": "", "CategoryID": 689, "SubCount": 6, "L.2.38": { "img": "", "CategoryID": 225, "SubCount": 14, "L.3.25": { "img": "", "CategoryID": 612, "SubCount": 0 }, "L.3.26": { "img": "", "CategoryID": 773, "SubCount": 0 }, "L.3.27": { "img": "", "CategoryID": 1174, "SubCount": 0 }, "L.3.28": { "img": "", "CategoryID": 135, "SubCount": 0 }, "L.3.29": { "img": "", "CategoryID": 451, "SubCount": 0 }, "L.3.30": { "img": "", "CategoryID": 1169, "SubCount": 0 }, "L.3.31": { "img": "", "CategoryID": 403, "SubCount": 0 }, "L.3.32": { "img": "", "CategoryID": 136, "SubCount": 0 }, "L.3.33": { "img": "", "CategoryID": 133, "SubCount": 0 }, "L.3.34": { "img": "", "CategoryID": 134, "SubCount": 0 }, "L.3.35": { "img": "", "CategoryID": 138, "SubCount": 0 }, "L.3.36": { "img": "", "CategoryID": 137, "SubCount": 0 }, "L.3.37": { "img": "", "CategoryID": 132, "SubCount": 0 }, "L.3.38": { "img": "", "CategoryID": 130, "SubCount": 0 } }, "L.2.53": { "img": "", "CategoryID": 217, "SubCount": 14, "L.3.40": { "img": "", "CategoryID": 108, "SubCount": 0 }, "L.3.41": { "img": "", "CategoryID": 611, "SubCount": 0 }, "L.3.42": { "img": "", "CategoryID": 772, "SubCount": 0 }, "L.3.43": { "img": "", "CategoryID": 448, "SubCount": 0 }, "L.3.44": { "img": "", "CategoryID": 103, "SubCount": 0 }, "L.3.45": { "img": "", "CategoryID": 404, "SubCount": 0 }, "L.3.46": { "img": "", "CategoryID": 106, "SubCount": 0 }, "L.3.47": { "img": "", "CategoryID": 105, "SubCount": 0 }, "L.3.48": { "img": "", "CategoryID": 107, "SubCount": 0 }, "L.3.49": { "img": "", "CategoryID": 104, "SubCount": 0 }, "L.3.50": { "img": "", "CategoryID": 266, "SubCount": 0 }, "L.3.51": { "img": "", "CategoryID": 1168, "SubCount": 0 }, "L.3.52": { "img": "", "CategoryID": 1173, "SubCount": 0 }, "L.3.53": { "img": "", "CategoryID": 112, "SubCount": 0 } }, "L.2.68": { "img": "", "CategoryID": 219, "SubCount": 14, "L.3.55": { "img": "", "CategoryID": 613, "SubCount": 0 }, "L.3.56": { "img": "", "CategoryID": 775, "SubCount": 0 }, "L.3.57": { "img": "", "CategoryID": 450, "SubCount": 0 }, "L.3.58": { "img": "", "CategoryID": 144, "SubCount": 0 }, "L.3.59": { "img": "", "CategoryID": 146, "SubCount": 0 }, "L.3.60": { "img": "", "CategoryID": 400, "SubCount": 0 }, "L.3.61": { "img": "", "CategoryID": 145, "SubCount": 0 }, "L.3.62": { "img": "", "CategoryID": 151, "SubCount": 0 }, "L.3.63": { "img": "", "CategoryID": 150, "SubCount": 0 }, "L.3.64": { "img": "", "CategoryID": 147, "SubCount": 0 }, "L.3.65": { "img": "", "CategoryID": 1170, "SubCount": 0 }, "L.3.66": { "img": "", "CategoryID": 1176, "SubCount": 0 }, "L.3.67": { "img": "", "CategoryID": 143, "SubCount": 0 }, "L.3.68": { "img": "", "CategoryID": 141, "SubCount": 0 } }, "L.2.83": { "img": "", "CategoryID": 220, "SubCount": 14, "L.3.70": { "img": "", "CategoryID": 614, "SubCount": 0 }, "L.3.71": { "img": "", "CategoryID": 774, "SubCount": 0 }, "L.3.72": { "img": "", "CategoryID": 1171, "SubCount": 0 }, "L.3.73": { "img": "", "CategoryID": 1175, "SubCount": 0 }, "L.3.74": { "img": "", "CategoryID": 453, "SubCount": 0 }, "L.3.75": { "img": "", "CategoryID": 153, "SubCount": 0 }, "L.3.76": { "img": "", "CategoryID": 399, "SubCount": 0 }, "L.3.77": { "img": "", "CategoryID": 154, "SubCount": 0 }, "L.3.78": { "img": "", "CategoryID": 155, "SubCount": 0 }, "L.3.79": { "img": "", "CategoryID": 161, "SubCount": 0 }, "L.3.80": { "img": "", "CategoryID": 160, "SubCount": 0 }, "L.3.81": { "img": "", "CategoryID": 156, "SubCount": 0 }, "L.3.82": { "img": "", "CategoryID": 159, "SubCount": 0 }, "L.3.83": { "img": "", "CategoryID": 158, "SubCount": 0 } }, "L.2.98": { "img": "", "CategoryID": 698, "SubCount": 14, "L.3.85": { "img": "", "CategoryID": 776, "SubCount": 0 }, "L.3.86": { "img": "", "CategoryID": 1167, "SubCount": 0 }, "L.3.87": { "img": "", "CategoryID": 253, "SubCount": 0 }, "L.3.88": { "img": "", "CategoryID": 608, "SubCount": 0 }, "L.3.89": { "img": "", "CategoryID": 473, "SubCount": 0 }, "L.3.90": { "img": "", "CategoryID": 256, "SubCount": 0 }, "L.3.91": { "img": "", "CategoryID": 258, "SubCount": 0 }, "L.3.92": { "img": "", "CategoryID": 259, "SubCount": 0 }, "L.3.93": { "img": "", "CategoryID": 260, "SubCount": 0 }, "L.3.94": { "img": "", "CategoryID": 254, "SubCount": 0 }, "L.3.95": { "img": "", "CategoryID": 255, "SubCount": 0 }, "L.3.96": { "img": "", "CategoryID": 262, "SubCount": 0 }, "L.3.97": { "img": "", "CategoryID": 1172, "SubCount": 0 }, "L.3.98": { "img": "", "CategoryID": 397, "SubCount": 0 } }, "L.2.100": { "img": "", "CategoryID": 218, "SubCount": 1, "L.3.100": { "img": "", "CategoryID": 208, "SubCount": 0 } } }, "L.1.122": { "img": "", "CategoryID": 870, "SubCount": 6, "L.2.108": { "img": "", "CategoryID": 871, "SubCount": 6, "L.3.103": { "img": "", "CategoryID": 883, "SubCount": 0 }, "L.3.104": { "img": "", "CategoryID": 885, "SubCount": 0 }, "L.3.105": { "img": "", "CategoryID": 873, "SubCount": 0 }, "L.3.106": { "img": "", "CategoryID": 886, "SubCount": 0 }, "L.3.107": { "img": "", "CategoryID": 872, "SubCount": 0 }, "L.3.108": { "img": "", "CategoryID": 884, "SubCount": 0 } }, "L.2.109": { "img": "", "CategoryID": 886, "SubCount": 0 }, "L.2.110": { "img": "", "CategoryID": 872, "SubCount": 0 }, "L.2.111": { "img": "", "CategoryID": 884, "SubCount": 0 }, "L.2.112": { "img": "", "CategoryID": 876, "SubCount": 0 }, "L.2.122": { "img": "", "CategoryID": 672, "SubCount": 9, "L.3.114": { "img": "", "CategoryID": 839, "SubCount": 0 }, "L.3.115": { "img": "", "CategoryID": 210, "SubCount": 0 }, "L.3.116": { "img": "", "CategoryID": 277, "SubCount": 0 }, "L.3.117": { "img": "", "CategoryID": 639, "SubCount": 0 }, "L.3.118": { "img": "", "CategoryID": 870, "SubCount": 0 }, "L.3.119": { "img": "", "CategoryID": 282, "SubCount": 0 }, "L.3.120": { "img": "", "CategoryID": 1191, "SubCount": 0 }, "L.3.121": { "img": "", "CategoryID": 932, "SubCount": 0 }, "L.3.122": { "img": "", "CategoryID": 332, "SubCount": 0 } } }, "L.1.171": { "img": "", "CategoryID": 672, "SubCount": 9, "L.2.124": { "img": "", "CategoryID": 839, "SubCount": 0 }, "L.2.125": { "img": "", "CategoryID": 210, "SubCount": 0 }, "L.2.132": { "img": "", "CategoryID": 277, "SubCount": 6, "L.3.127": { "img": "1173128.Chat Icon.png", "CategoryID": 1211, "SubCount": 0 }, "L.3.128": { "img": "", "CategoryID": 1159, "SubCount": 0 }, "L.3.129": { "img": "", "CategoryID": 1084, "SubCount": 0 }, "L.3.130": { "img": "", "CategoryID": 1150, "SubCount": 0 }, "L.3.131": { "img": "", "CategoryID": 1138, "SubCount": 0 }, "L.3.132": { "img": "", "CategoryID": 1119, "SubCount": 0 } }, "L.2.145": { "img": "", "CategoryID": 639, "SubCount": 12, "L.3.134": { "img": "", "CategoryID": 122, "SubCount": 0 }, "L.3.135": { "img": "", "CategoryID": 121, "SubCount": 0 }, "L.3.136": { "img": "", "CategoryID": 512, "SubCount": 0 }, "L.3.137": { "img": "", "CategoryID": 124, "SubCount": 0 }, "L.3.138": { "img": "", "CategoryID": 125, "SubCount": 0 }, "L.3.139": { "img": "", "CategoryID": 127, "SubCount": 0 }, "L.3.140": { "img": "", "CategoryID": 878, "SubCount": 0 }, "L.3.141": { "img": "", "CategoryID": 877, "SubCount": 0 }, "L.3.142": { "img": "", "CategoryID": 880, "SubCount": 0 }, "L.3.143": { "img": "", "CategoryID": 879, "SubCount": 0 }, "L.3.144": { "img": "", "CategoryID": 874, "SubCount": 0 }, "L.3.145": { "img": "", "CategoryID": 282, "SubCount": 0 } }, "L.2.152": { "img": "", "CategoryID": 870, "SubCount": 6, "L.3.147": { "img": "", "CategoryID": 871, "SubCount": 0 }, "L.3.148": { "img": "", "CategoryID": 886, "SubCount": 0 }, "L.3.149": { "img": "", "CategoryID": 872, "SubCount": 0 }, "L.3.150": { "img": "", "CategoryID": 884, "SubCount": 0 }, "L.3.151": { "img": "", "CategoryID": 876, "SubCount": 0 }, "L.3.152": { "img": "", "CategoryID": 672, "SubCount": 0 } }, "L.2.158": { "img": "", "CategoryID": 282, "SubCount": 5, "L.3.154": { "img": "", "CategoryID": 122, "SubCount": 0 }, "L.3.155": { "img": "", "CategoryID": 512, "SubCount": 0 }, "L.3.156": { "img": "", "CategoryID": 121, "SubCount": 0 }, "L.3.157": { "img": "", "CategoryID": 124, "SubCount": 0 }, "L.3.158": { "img": "", "CategoryID": 127, "SubCount": 0 } }, "L.2.165": { "img": "", "CategoryID": 1191, "SubCount": 6, "L.3.160": { "img": "", "CategoryID": 1184, "SubCount": 0 }, "L.3.161": { "img": "", "CategoryID": 1186, "SubCount": 0 }, "L.3.162": { "img": "", "CategoryID": 1185, "SubCount": 0 }, "L.3.163": { "img": "", "CategoryID": 1187, "SubCount": 0 }, "L.3.164": { "img": "", "CategoryID": 1189, "SubCount": 0 }, "L.3.165": { "img": "", "CategoryID": 1190, "SubCount": 0 } }, "L.2.170": { "img": "", "CategoryID": 932, "SubCount": 4, "L.3.167": { "img": "", "CategoryID": 936, "SubCount": 0 }, "L.3.168": { "img": "", "CategoryID": 1181, "SubCount": 0 }, "L.3.169": { "img": "", "CategoryID": 331, "SubCount": 0 }, "L.3.170": { "img": "", "CategoryID": 1182, "SubCount": 0 } }, "L.2.171": { "img": "", "CategoryID": 332, "SubCount": 0 } } }}
function make_lookup(obj, map_obj = {}) {
let children = Object.keys(obj).filter(k => k.startsWith('L.'))
if (!children) return map_obj
children.forEach(c => {
map_obj[c] = obj[c]
make_lookup(obj[c], map_obj)
})
return map_obj
}
let myMap = make_lookup(OBJ['root'])
// lookup L.2.170
console.log(myMap['L.2.170'])
// lookup L.3.169
console.log(myMap['L.3.169'])
将上面的内容转换为返回键数组而不是对象本身的东西应该不难,但不清楚你想要什么。
关于javascript - 导航 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48024331/
最近开始学习MongoDB。今天老师教了我们 mongoexport 命令。在练习时,我遇到了一个典型的问题,包括教练在内的其他同学都没有遇到过。我在我的 Windows 10 机器上使用 Mongo
我是 JSON Schema 的新手,读过什么是 JSON Schema 等等。但我不知道如何将 JSON Schema 链接到 JSON 以针对该 JSON Schema 进行验证。谁能解释一下?
在 xml 中,我可以在另一个 xml 文件中包含一个文件并使用它。如果您的软件从 xml 获取配置文件但没有任何方法来分离配置,如 apache/ngnix(nginx.conf - site-av
我有一个 JSON 对象,其中包含一个本身是 JSON 对象的字符串。我如何反序列化它? 我希望能够做类似的事情: #[derive(Deserialize)] struct B { c: S
考虑以下 JSON { "a": "{\"b\": 12, \"c\": \"test\"}" } 我想定义一个泛型读取 Reads[Outer[T]]对于这种序列化的 Json import
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 11 个月前关闭。 Improve
我的旧项目在 MySQL 中有 Standard JSON 格式的数据。 对于我在 JS (Node.js) 和 DynamoDB 中的全新项目,关于 Standard JSON格式: 是否建议将其转
JSON 值字符串、数字、true、false、null 是否是有效的 JSON? 即,是 true 一个有效的 JSON 文档?还是必须是数组/对象? 一些验证器接受这个(例如 http://jso
我有一个 JSON 字符串,其中一个字段是文本字段。这个文本字段可以包含用户在 UI 中输入的文本,如果他们输入的文本是 JSON 文本,也许是为了说明一些编码,我需要对他们的文本进行编码,以便它不会
我正在通过 IBM MQ 调用处理数据,当由 ColdFusion 10 (10,0,11,285437) 序列化时,0 将作为 +0.0 返回,它会导致无效的 JSON并且无法反序列化。 stPol
我正在从三个数组中生成一个散列,然后尝试构建一个 json。我通过 json object has array 成功了。 require 'json' A = [['A1', 'A2', 'A3'],
我从 API 接收 JSON,响应可以是 30 种类型之一。每种类型都有一组唯一的字段,但所有响应都有一个字段 type 说明它是哪种类型。 我的方法是使用serde .我为每种响应类型创建一个结构并
我正在下载一个 JSON 文件,我已将其检查为带有“https://jsonlint.com”的有效 JSON 到文档目录。然后我打开文件并再次检查,结果显示为无效的 JSON。这怎么可能????这是
我正在尝试根据从 API 接收到的数据动态创建一个 JSON 对象。 收到的示例数据:将数据解码到下面给出的 CiItems 结构中 { "class_name": "test", "
我想从字符串转换为对象。 来自 {"key1": "{\n \"key2\": \"value2\",\n \"key3\": {\n \"key4\": \"value4\"\n }\n
目前我正在使用以下代码将嵌套的 json 转换为扁平化的 json: import ( "fmt" "github.com/nytlabs/gojsonexplode" ) func
我有一个使用来自第三方 API 的数据的应用程序。我需要将 json 解码为一个结构,这需要该结构具有“传入”json 字段的 json 标签。传出的 json 字段具有不同的命名约定,因此我需要不同
我想使用 JSON 架构来验证某些值。我有两个对象,称它们为 trackedItems 和 trackedItemGroups。 trackedItemGroups 是组名称和 trackedItem
考虑以下案例类模式, case class Y (a: String, b: String) case class X (dummy: String, b: Y) 字段b是可选的,我的一些数据集没有字
我正在存储 cat ~/path/to/file/blah | 的输出jq tojson 在一个变量中,稍后在带有 JSON 内容的 curl POST 中使用。它运作良好,但它删除了所有换行符。我知
我是一名优秀的程序员,十分优秀!