使用localStorage时使用嵌套JSON可靠吗?
在这里真的很困惑。在我当前的项目中,所有数据都存储在LocalStorage中,所有对象/数组都具有最大的嵌套水平。这导致了许多不便,甚至相同实体的重复。我决定将数据结构重构为更方便,但也更加嵌套。
对于json.stringify()
/json.parse()
来回这些数据,它会安全可靠吗?一些Google搜索结果指出,JSON不支持嵌套数组,但确实支持它们!或者..?
我现在拥有的:
cars: [{id: car1, brand: 'bmw'}, {id: car2, brand: 'ford'}, {id: car3, brand: 'dodge'}]
car1_details: {id: car1, color: 'blue', horsepower: 150, type: 'coupe']
car2_details: {id: car2, color: 'red', horsepower: 180, type: 'hatchback']
car3_details: {id: car3, color: 'yellow', horsepower: 200, type: 'sedan']
我显然想要的:
cars: [
{
id: car1,
brand: 'bmw',
details: {
color: 'blue',
horsepower: 150,
type: 'coupe'
}
//etc
]
我进行了测试,似乎100%工作,但只是想通过问你们来确保。 还有 this 从那以后已经固定了。
Really confused here. On my current project all data is stored in localStorage, and all objects/arrays have maximum one level of nesting. That results in many inconveniences and even duplicates of the same entities. I decided to refactor the data structure to something more convenient, but also more nested.
Would it be safe and reliable to JSON.stringify()
/ JSON.parse()
back and forth such data? Some of google search results states that json does not support nested arrays, but it does support them! Or..?
what I have now:
cars: [{id: car1, brand: 'bmw'}, {id: car2, brand: 'ford'}, {id: car3, brand: 'dodge'}]
car1_details: {id: car1, color: 'blue', horsepower: 150, type: 'coupe']
car2_details: {id: car2, color: 'red', horsepower: 180, type: 'hatchback']
car3_details: {id: car3, color: 'yellow', horsepower: 200, type: 'sedan']
what I obviously want:
cars: [
{
id: car1,
brand: 'bmw',
details: {
color: 'blue',
horsepower: 150,
type: 'coupe'
}
//etc
]
I tested and it seems to be 100% working, but just wanted to make sure by asking you guys.
Also there is this outdated post, but apparently it has been fixed since then.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论