简体   繁体   English

如何使用Javascript将字符串解析为数组的数组

[英]How to parse a string to an array of array using Javascript

I am trying to use JSON.parse to parse a string to the array of array, such as 我正在尝试使用JSON.parse将字符串解析为数组的数组,例如

var a = "[['1909-23', 'egg']]"  
JSON.parse(a)

It gives me the SytaxError. 它给了我SytaxError。 Wonder if there is any easy way to solve it. 想知道是否有任何简单的方法来解决它。 Thanks. 谢谢。

The string 字符串

"[['1909-23', 'egg']]"

Is not a valid JSON string. 不是有效的JSON字符串。 As such you can't call JSON.parse() on it. 因此,您无法在其上调用JSON.parse()

The JSON format requires double quotes around strings. JSON格式需要在字符串两边加上双引号。

A solution would be then to use double quotes: 一个解决方案是使用双引号:

 var a = '[["1909-23", "egg"]]'; console.log(JSON.parse(a)); 

Before you use this, please read Why is using the JavaScript eval function a bad idea? 在使用此功能之前,请阅读为什么使用JavaScript eval函数不是一个好主意? . This will potentially open up your JavaScript to code injection attacks . 这可能会打开JavaScript进行代码注入攻击 A much better solution is to actually turn your string into correct JSON and parse is using JSON.parse 更好的解决方案是将字符串实际转换为正确的JSON并使用JSON.parse解析

That all said, you can “parse” (actually you've executing the string as javascript, hence the injection problem) this string using eval . 综上所述,您可以使用eval “解析”(实际上您已将字符串作为javascript 执行 ,因此是注入问题)。

 var a = "[['1909-23', 'egg']]" var b = eval(a); console.log(b); 

Note the warning on MDN 注意MDN上警告

Do not ever use eval! 永远不要使用eval!

eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. eval()是一个危险的函数,它以调用者的特权执行它传递的代码。 If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. 如果使用可能受到恶意方影响的字符串运行eval(),则最终可能会在网页/扩展名的许可下在用户计算机上运行恶意代码。 More importantly, a third-party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways to which the similar Function is not susceptible. 更重要的是,第三方代码可以看到eval()的调用范围,这可能导致以类似功能不易受到攻击的方式进行攻击。

eval() is also slower than the alternatives, since it has to invoke the JS interpreter, while many other constructs are optimized by modern JS engines. eval()的速度也比其他方法慢,因为它必须调用JS解释器,而许多其他结构是由现代JS引擎优化的。

Additionally, modern javascript interpreters convert javascript to machine code. 另外,现代的javascript解释器将javascript转换为机器代码。 This means that any concept of variable naming gets obliterated. 这意味着任何变量命名的概念都将被废除。 Thus, any use of eval will force the browser to do long expensive variable name lookups to figure out where the variable exists in the machine code and set it's value. 因此,任何对eval的使用都将迫使浏览器进行长时间的昂贵的变量名查找,以找出变量在机器代码中的位置并设置其值。 Additonally, new things can be introduced to that variable through eval() such as changing the type of that variable, forcing the browser to reevaluate all of the generated machine code to compensate. 另外,可以通过eval()向该变量引入新事物,例如更改该变量的类型,迫使浏览器重新评估所有生成的机器代码以进行补偿。 However, there (thankfully) exists a very good alternative to eval: simply using window.Function. 但是,(值得庆幸的),有一个非常好的替代eval的方法:简单地使用window.Function。 As an example of how you convert code using evil eval() to using Function(), 作为如何将使用邪恶的eval()转换为使用Function()的代码的示例,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM