简体   繁体   English

(javascript) JSON.parse() 更改 json 的内容

[英](javascript) JSON.parse() changes content of json

I'm making a practice tool for a game that helps you to learn how to play songs with the piano and what i'm trying to is get all the notes that are pressed within 50ms from one another and make them display as "you have to press them together" now, this seems to be working but when i pass the string of the song to the parser, it changes the content of the array.我正在为一款游戏制作一个练习工具,帮助你学习如何用钢琴弹奏歌曲,而我想要的是让所有在 50 毫秒内按下的音符相互显示为“你有将它们压在一起”现在,这似乎正在工作,但是当我将歌曲的字符串传递给解析器时,它会改变数组的内容。 i've used the exact same method to parse the object in other functions and it works as expected, here not, this is (part) of the unparsed array我已经使用完全相同的方法在其他函数中解析 object 并且它按预期工作,这里不是,这是未解析数组的(部分)

[{"time":2937,"key":"Key2"},
{"time":2943,"key":"Key7"},
{"time":2949,"key":"Key0"},
{"time":4297,"key":"Key6"},
{"time":4306,"key":"Key11"},
{"time":4315,"key":"Key2"},
{"time":5547,"key":"Key5"},
{"time":5549,"key":"Key2"},
{"time":5554,"key":"Key7"},
{"time":7480,"key":"Key8"},
{"time":7814,"key":"Key9"},

this is after being parsed这是在解析之后

0: {time: 2943, key: "Key7"}
1: {time: 4297, key: "Key6"}
2: {time: 4306, key: "Key11"}
3: {time: 4315, key: "Key2"}
4: {time: 5547, key: "Key5"}
5: {time: 5549, key: "Key2"}
6: {time: 5554, key: "Key7"}
7: {time: 7480, key: "Key8"}
8: {time: 7814, key: "Key9"}
9: {time: 8158, key: "Key5"}
10: {time: 8166, key: "Key10"}

why does this happen and how can i fix it?为什么会发生这种情况,我该如何解决? the code is litterally:代码是字面意思:

console.log(song)
trainingSong = JSON.parse(song)
console.log(trainingSong)

Use JSON.stringify to stringify the code & then use JSON.parse to parse it.使用JSON.stringify对代码进行字符串化,然后使用JSON.parse进行解析。

 console.log( JSON.stringify([ { time: 2937, key: "Key2" }, { time: 2943, key: "Key7" }, { time: 2949, key: "Key0" }, { time: 4297, key: "Key6" }, { time: 4306, key: "Key11" }, { time: 4315, key: "Key2" }, { time: 5547, key: "Key5" }, { time: 5549, key: "Key2" }, { time: 5554, key: "Key7" }, { time: 7480, key: "Key8" }, { time: 7814, key: "Key9" }, ]) );

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

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