英文:
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将[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:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论