简体   繁体   English

JSON.parse()Reviver函数的奇怪行为

[英]Strange behavior of JSON.parse() reviver function

I am using second parameter of JSON.parse() to modify the result, but I don't quite clear about the order of the function parameter and also how it work 我正在使用JSON.parse()的第二个参数来修改结果,但是我不太清楚函数参数的顺序及其工作方式

I have read the document about the using of the reviver function (eg https://www.ecma-international.org/ecma-262/6.0/#sec-json.parse and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse ). 我已经阅读了有关使用reviver函数的文档(例如https://www.ecma-international.org/ecma-262/6.0/#sec-json.parsehttps://developer.mozilla.org/en -US / docs / Web / JavaScript / Reference / Global_Objects / JSON / parse )。 From what I understand, this function would work with object, and first parameter is key or property name, second function is value or property value. 据我了解,此功能将与对象一起使用,第一个参数是键或属性名称,第二个功能是值或属性值。 What I don't understand is the return value of the function. 我不明白的是函数的返回值。

This is what it is done in example 这是示例中所做的

 var obj1 = '{"a":1, "b":42}'; let text = JSON.parse(obj1, (key, value) => { if (typeof value === 'number'){ return value * 2 } else{ return value } } ) console.log(text) // {"a": 2, "b": 84} 

This work well. 这个工作很好。 But when I try to modify the code since I know all value is number already 但是当我尝试修改代码时,因为我知道所有值都已经是数字

 var obj1 = '{"a":1, "b":42}'; let text = JSON.parse(obj1, (key, value) =>{ return value * 2 }) console.log(text) // NaN 

It is strange to me why when I delete the return value it doesn't work. 对于我来说很奇怪,为什么当我删除return value它不起作用。 I mean, with the function none of the value return undefined when I value*2 it. 我的意思是,对于该函数,当我对value*2运算时,没有一个值返回未定义。 I then try another test 然后,我尝试另一个测试

  var obj1 = '{"a":1, "b":42}'; let text = JSON.parse(obj1, (key, value) => { if (typeof value === 'number'){ console.log('This is in if',key, value) return value * 2 } else{ console.log('This is in else', key, value) return value } } ) console.log(text) 

Another strange thing happen when the code in else statement run even when it suppose not to run because the condition is incorrect. else语句中的代码即使由于条件不正确而假定不运行时,也会发生另一个奇怪的事情。 And when it run it even print out the obj1 object, which I didn't include in the statement. 并且当它运行时,它甚至会打印出obj1对象,我没有在语句中包含该对象。

Because it will also iterate thorough the object which is {"a":1, "b":42} .It will start from most nested level and then will go the original value itself which is {"a":1, "b":42} . 因为它还会遍历{"a":1, "b":42}它将从大多数嵌套级别开始,然后将返回原始值本身,即{"a":1, "b":42}

According to MDN 根据MDN

If a reviver is specified, the value computed by parsing is transformed before being returned. 如果指定了Reviver,则通过解析计算的值将在返回之前进行转换。 Specifically, the computed value and all its properties (beginning with the most nested properties and proceeding to the original value itself) are individually run through the reviver 具体而言,计算值及其所有属性(从嵌套最多的属性开始,一直到原始值本身)都通过齐整器运行

By the way you can shorten your function. 顺便说一下,您可以缩短功能。

 var obj1 = '{"a":1, "b":42}'; let text = JSON.parse(obj1, (_, value) => value * 2 || value) console.log(text) 

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

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