JSON.parse将数字值更改为null

huangapple go评论66阅读模式
英文:

JSON.parse changing number values to null

问题

解析一个包含嵌套数组的字符串JSON对象会将数组中的数字置为null。

我正在使用JavaScript解析一个字符串JSON对象。

字符串内容:(如果需要,可以将其输入https://jsonformatter.org/json-pretty-print,但它非常简单)

{"teams":[["Roman Bravo-Young (PSU)","vito Arujau (COR)"],["Spencer Lee (IOWA)","Pat GLory (PRIN)"],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null]],"results":[[[[33,0],[1,0],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null]],[[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null]],[[null,null],[null,null],[null,null],[null,null]],[[null,null],[null,null]],[[null,null],[null,null]]]]}

代码:

console.log(brackets[id].data);
var adminData = JSON.parse(brackets[id].data);
console.log("ADMIN DATA");
console.log(adminData);

控制台输出:

JSON.parse将数字值更改为null

JSON.parse将[33, 0]和[1, 0]数组转换为null值。在我几十年的编程经验中,我从未见过这种情况。

更奇怪的是:再次调用JSON.parse(brackets[id].data);将导致控制台中的第一次调用显示正确数据,但控制台中的第二次日志仍然显示所有内容为null。

这是如何发生的?谢谢!

英文:

Parsing a string JSON object nullifies numbers that are in nested arrays.

I'm parsing a string JSON object in javascript.

The string: (throw it into https://jsonformatter.org/json-pretty-print if you want, but its pretty simple)

{"teams":[["Roman Bravo-Young (PSU)","vito Arujau (COR)"],["Spencer Lee (IOWA)","Pat GLory (PRIN)"],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null]],"results":[[[[33,0],[1,0],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null]],[[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null]],[[null,null],[null,null],[null,null],[null,null]],[[null,null],[null,null]],[[null,null],[null,null]]]]}

The code:

console.log(brackets[id].data);
var adminData = JSON.parse(brackets[id].data);
console.log("ADMIN DATA");
console.log(adminData);

The console output:

JSON.parse将数字值更改为null

The JSON.parse is turning the [33, 0] and [1, 0] arrays into null values. I have never seen this before in my decades of programming.

Even stranger: Doing a second JSON.parse(brackets[id].data); call will cause the FIRST call in the console to show the correct data, but the second log in the console will still show everything as null.

How can this happen? Thank you!

答案1

得分: 1

我将这个数组作为引用传递给一个新变量,然后对新变量进行了修改,但因为它是一个引用,它也影响了原始变量。在记录之后,Chrome显示对象已被修改。

问题变量突出显示的屏幕截图,稍后会修改引用

英文:

I was passing this array as a reference into a new variable, which was being modified, but because it was a reference, it affected the original variable as well. Chrome was showing the object modified AFTER the log.

Screenshot with the problem variable highlighted, which modifies the reference later

huangapple
  • 本文由 发表于 2023年3月4日 04:16:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定