如何解析包含“NaN”的JSON字符串在Node.js中 [英] How to parse JSON string containing "NaN" in Node.js

查看:325
本文介绍了如何解析包含“NaN”的JSON字符串在Node.js中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个接收包含文字NaN的JSON数据字符串的node.js应用程序,如

 [ 2,3,NaN,5,6]

这会导致 JSON崩溃。在Node.js中解析(...)>。我想解析它,如果我可以进入一个对象。



我知道 NaN 不属于JSON规范。大多数SO链接(在json中发送NaN )建议修复输出。



在这里,虽然数据是在我无法控制的服务器中生成的,但它是由商业Java库提供的,我可以在其中查看源代码。它由Google的Gson库生成:

  private Gson gson =(new GsonBuilder()。serializeSpecialFloatingPointValues()。create()) ; 
...
gson.toJson(data [i],Vector.class,jsonOut)

这似乎是一个合法的来源。并根据


JSON规范的2.4节不允许使用特殊字符双值
(NaN,Infinity,-Infinity)。但是,Javascript规范(请参阅
第4.3.20,4.3.22,4.3.23节)允许将这些值作为有效的
Javascript值。而且,大多数JavaScript引擎都会在JSON中接受这些
的特殊值,而不会出现问题。因此,在实践层面上,即使JSON
规范不允许它们,
也可以接受这些值作为有效的JSON。


尽管如此,Node.js和Chrome都失败了: JSON.parse('[1,2,3,NaN,5]')



有没有可以在JSON.parse()中设置的标志?或者接受 NaN 作为文字的替代解析器?



我一直在使用谷歌搜索虽然似乎无法在这个问题上找到文档。



PHP:如何将无限或NaN编码编码为JSON? $ b

有一个接收包含文字NaN的JSON数据字符串的node.js应用程序,如

然后您的NodeJS app 不是接收 JSON ,它接收的文字有点类似JSON。 NaN 不是有效的JSON令牌。



三个选项:

1。获取源码以正确生成JSON



这显然是首选课程。数据不是JSON,应该修复,这可以解决您的问题。



2。以简单的方式容忍 NaN



您可以用替换它> null 解析它之前,例如:

  var result = JSON.parse(yourString.replace(/ \\ bNaN \b / g,null)); 

...然后处理 null s在结果中。但是这非常简单,它不允许字符 NaN 可能出现在某个字符串中。



或者,转动Matt Ball的 reviver idea (现在已删除),您可以将其更改为特殊的字符串(如*** NaN ***),然后使用reviver将其替换为真正的 NaN

  var result = JSON.parse(yourString.replace(/ \NaN\b / g,'*** NaN *** '),函数(key,value){
返回值===*** NaN ***?NaN:value;
});

...但是这个问题有点简单,假设字符 NaN 永远不会出现在合适的地方。



3。使用(shudder!) eval



并且信任这些数据的来源,并且不会有任何被篡改的可能性 ,那么您可以使用 eval 来解析它,而不是 JSON.parse 。由于 eval 允许使用完整的JavaScript语法,包括 NaN 。希望我提出了足够的警告,让人们明白,我只会在非常非常非常小的情况下推荐这一点。但是,请记住 eval 允许任意执行代码,所以如果字符串被篡改的可能性不要使用它。


Have a node.js app that is receiving JSON data strings that contain the literal NaN, like

 "[1, 2, 3, NaN, 5, 6]"

This crashes JSON.parse(...) in Node.js. I'd like to parse it, if i can into an object.

I know NaN is not part of JSON spec. Most SO links (sending NaN in json) suggest to fix the output.

Here, though the data is produced in a server I don't control, it's by a commercial Java library where I can see the source code. And it's produced by Google's Gson library:

private Gson gson = (new GsonBuilder().serializeSpecialFloatingPointValues().create()); 
... 
gson.toJson(data[i], Vector.class, jsonOut)

So that seems like a legitimate source. And according to the Gson API Javadoc it says I should be able to parse it:

Section 2.4 of JSON specification disallows special double values (NaN, Infinity, -Infinity). However, Javascript specification (see section 4.3.20, 4.3.22, 4.3.23) allows these values as valid Javascript values. Moreover, most JavaScript engines will accept these special values in JSON without problem. So, at a practical level, it makes sense to accept these values as valid JSON even though JSON specification disallows them.

Despite that, this fails in both Node.js and Chrome: JSON.parse('[1,2,3,NaN,"5"]')

Is there a flag to set in JSON.parse()? Or an alternative parser that accepts NaN as a literal?

I've been Googling for a while but can't seem to find a doc on this issue.

PHP: How to encode infinity or NaN numbers to JSON?

解决方案

Have a node.js app that is receiving JSON data strings that contain the literal NaN, like

Then your NodeJS app isn't receiving JSON, it's receiving text that's vaguely JSON-like. NaN is not a valid JSON token.

Three options:

1. Get the source to correctly produce JSON

This is obviously the preferred course. The data is not JSON, that should be fixed, which would fix your problem.

2. Tolerate the NaN in a simple-minded way:

You could replace it with null before parsing it, e.g.:

var result = JSON.parse(yourString.replace(/\bNaN\b/g, "null"));

...and then handle nulls in the result. But that's very simple-minded, it doesn't allow for the possibility that the characters NaN might appear in a string somewhere.

Alternately, spinning Matt Ball's reviver idea (now deleted), you could change it to a special string (like "***NaN***") and then use a reviver to replace that with the real NaN:

var result = JSON.parse(yourString.replace(/\bNaN\b/g, '"***NaN***"'), function(key, value) {
    return value === "***NaN***" ? NaN : value;
});

...but that has the same issue of being a bit simple-minded, assuming the characters NaN never appear in an appropriate place.

3. Use (shudder!) eval

If you know and trust the source of this data and there's NO possibility of it being tampered with in transit, then you could use eval to parse it instead of JSON.parse. Since eval allows full JavaScript syntax, including NaN, that works. Hopefully I made the caveat bold enough for people to understand that I would only recommend this in a very, very, very tiny percentage of situations. But again, remember eval allows arbitrary execution of code, so if there's any possibility of the string having been tampered with, don't use it.

这篇关于如何解析包含“NaN”的JSON字符串在Node.js中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆