简体   繁体   English

如何解码类似JSON的JavaScript对象文字字符串

[英]How to decode JSON-like JavaScript Object Literal Strings

I'm having trouble decoding an array of strings from what I thought was JSON. 我无法从我认为是JSON的字符串数组中解码。

var result = [
  "{gene: 'PEX2', go_bp: '0.766500871709', CombinedPvalue: '9.999999995E-4'}",
  "{gene: 'PEX5', go_bp: '0.766472586087', CombinedPvalue: '9.999999995E-4'}",
  "{gene: 'PEX7', go_bp: '0.766386859737', CombinedPvalue: '9.999999995E-4'}"
];

You can see that there are 3 gene-related strings of JavaScript object literals, each encoded as a string. 您会看到,有3个与基因相关的JavaScript对象文字字符串,每个都编码为字符串。 How can I decode these? 我该如何解码?

I tried JSON.parse but it gives me an error. 我尝试了JSON.parse但它给了我一个错误。

for (var i = 0; i < result.length; i++) 
    console.log(JSON.parse(result[i]));

Uncaught SyntaxError: Unexpected token g . Uncaught SyntaxError: Unexpected token g

Is there a simple way? 有没有简单的方法?

What you have is not JSON, but Javascript objects in text form. 您拥有的不是JSON,而是文本形式的Javascript对象。 You can convert them to Javascript objects with eval() : 您可以使用eval()将它们转换为Javascript对象:

  var result = [ "{gene: 'PEX2', go_bp: '0.766500871709', CombinedPvalue: '9.999999995E-4'}", "{gene: 'PEX5', go_bp: '0.766472586087', CombinedPvalue: '9.999999995E-4'}", "{gene: 'PEX7', go_bp: '0.766386859737', CombinedPvalue: '9.999999995E-4'}" ]; var f; for (var i = 0; i < result.length; i++) { eval("f = "+result[i]); console.log(f.gene); } 

Note: eval is generally held to be evil. 注意:通常认为eval是邪恶的。 In this case it's safe enough if you're absolutely sure that the source array will only ever hold data, and no code. 在这种情况下,如果您完全确定源数组将仅保存数据而没有代码,则这是足够安全的。

Since this is valid javascript, you can use Function() to return a new instance of the object by creating an anonymous function and then immediately executing it. 由于这是有效的javascript,因此您可以使用Function()通过创建匿名函数然后立即执行它来返回对象的新实例。 Unlike the other answer with eval() , you don't have to declare a variable and assign the object literal to that variable in the string passed to eval - everything you need can be done cleanly in one line. 与使用eval()的其他答案不同,您不必声明变量并将对象常量分配给传递给eval的字符串中的该变量-您所需的所有操作都可以在一行中完成。

 var result = [ "{gene: 'PEX2', go_bp: '0.766500871709', CombinedPvalue: '9.999999995E-4'}", "{gene: 'PEX5', go_bp: '0.766472586087', CombinedPvalue: '9.999999995E-4'}", "{gene: 'PEX7', go_bp: '0.766386859737', CombinedPvalue: '9.999999995E-4'}" ]; for (var i = 0; i < result.length; i++) { // create function that returns the obj literal // and call it immedieately. var obj = new Function( 'return ' + result[i] + ';' )(); document.write('gene: ' + obj.gene + ' <br />'); } 

The JSON format requires double quotes around property names. JSON格式需要在属性名称两边加上双引号。 Your sample data lacks these quotes, and this is not valid JSON. 您的样本数据缺少这些引号,并且这不是有效的JSON。

It also requires double quoted values, not single quoted. 它还需要双引号,而不是单引号。

Try something like this: 尝试这样的事情:

   '{"gene": "PEX2", "go_bp": "0.766500871709", "CombinedPvalue": "9.999999995E-4"}',

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

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