-1
var temp = { "100" = ["submenu 4", "submenu 5", "submenu 6"],
        "136" = ["submenu 10", "submenu 11", "submenu 12"],
        "125" = ["submenu 19", "submenu 20", "submenu 21"],
        "99"  = ["submenu 16", "submenu 17", "submenu 18"], 
        "77"  = ["submenu 13", "submenu 14", "submenu 15"],
        "70"  = ["submenu 1", "submenu 2", "submenu 3"],
        "4"  = ["submenu 7", "submenu 8", "submenu 9"] };

如何将=替换为:符号?或者如何在 jquery 函数中迭代上述 temp?

请给出解决方案。

4

3 回答 3

1

此 JSON 无效,您无法替换 temp 中的 '=',因为 temp 会在尝试将此值分配给它时引发错误。在您的代码中替换它以使其工作,否则您无法对此进行迭代。

于 2012-10-15T14:43:58.270 回答
1
var temp = '{ "100" = ["submenu 4", "submenu 5", "submenu 6"],
        "136" = ["submenu 10", "submenu 11", "submenu 12"],
        "125" = ["submenu 19", "submenu 20", "submenu 21"],
        "99"  = ["submenu 16", "submenu 17", "submenu 18"], 
        "77"  = ["submenu 13", "submenu 14", "submenu 15"],
        "70"  = ["submenu 1", "submenu 2", "submenu 3"],
        "4"  = ["submenu 7", "submenu 8", "submenu 9"] }';
temp = temp.replace(/=/g,":");
temp = JSON.parse(temp);

像这样的东西?

于 2012-10-15T14:45:20.330 回答
0

=除了将符号更改为 之外,没有有效的解决方案:。在那一点上完全停止。从字面上看,这是真正解决此问题并使其成为有效 JavaScript 对象的唯一方法。

如果您从外部来源获取此信息,则可以对其进行一些正则表达式更改。如果是您的代码,您只需更改符号即可。

于 2012-10-15T14:48:58.847 回答