简体   繁体   English

Javascript - 如何防止 localStorage 将数据存储为字符串?

[英]Javascript - How to prevent localStorage from storing data as a string?

I'm currently using a set of data that is an array of objects.我目前正在使用一组数据,它是一个对象数组。 In order to try and speed up load times, I'm trying to use localStorage to store the data and access it quickly.为了尝试加快加载时间,我尝试使用 localStorage 来存储数据并快速访问它。 The big problem is that the data isn't being stored as an array of objects but rather as a string.最大的问题是数据不是作为对象数组存储,而是作为字符串存储。 The data itself looks something like this.数据本身看起来像这样。

[{id: 1, name: "BP", industry: "oil and gas", indicator: "Board Oversight"}, 
{id: 2, name: "Shell", industry: "oil and gas", indicator: "Board Oversight"}]

This entire array is being stored in a variable called dataSet and I attempted to store it into local storage like so.整个数组被存储在一个名为dataSet的变量中,我试图像这样将它存储到本地存储中。

localStorage.setItem('finalDataset', dataSet)

When I try to get the item, the data stored inside looks correct but the data is a string as opposed to what I actually want.当我尝试获取该项目时,存储在其中的数据看起来是正确的,但数据是一个字符串,而不是我真正想要的。 So how can I store this data inside of localStorage in the exact same format?那么如何以完全相同的格式将这些数据存储在 localStorage 中呢?

I also attempted to do localStorage.setItem('finalDataset', JSON.stringify(dataSet)) and that also returned a string.我还尝试做localStorage.setItem('finalDataset', JSON.stringify(dataSet))并且还返回了一个字符串。

Javascript - How to prevent localStorage from storing data as a string? Javascript - 如何防止 localStorage 将数据存储为字符串?

You can't, that's what it does.你不能,这就是它的作用。

What you can do is use a string-friendly serialization format, like JSON.可以做的是使用字符串友好的序列化格式,例如 JSON。

To retrieve:要检索:

const data = JSON.parse(localStorage.getItem("name") || "null");

That retrieves and deserializes the data.这会检索和反序列化数据。 You get null if there wasn't anything stored for that key.如果没有为该密钥存储任何内容,您将获得null (Technically, you don't even need the || "null" part of that, since localStorage.getItem("name") returns null and JSON.parse coerces its argument to a string, but...I like to include it anyway, for...clarity?) (从技术上讲,您甚至不需要|| "null"部分,因为localStorage.getItem("name")返回nullJSON.parse将其参数强制转换为字符串,但是...我喜欢包含它无论如何,为了……清晰?)

To store:储藏:

localStorage.setItem("name", JSON.stringify(data));

The answer is simple: You cannot store it as a different type than a string value.答案很简单:您不能将其存储为与字符串值不同的类型。

Extract from the API:摘自 API:

The keys and the values are always in the UTF-16 DOMString format, which uses two bytes per character.键和值始终采用 UTF-16 DOMString 格式,每个字符使用两个字节。 As with objects, integer keys are automatically converted to strings.与对象一样,integer 键会自动转换为字符串。

Source: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage来源: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

You can't... The data is always saved as a string.你不能... 数据始终保存为字符串。

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

"The keys and the values are always in the UTF-16 DOMString format, which uses two bytes per character. As with objects, integer keys are automatically converted to strings." “键和值始终采用 UTF-16 DOMString 格式,每个字符使用两个字节。与对象一样,integer 键会自动转换为字符串。”

The trick is to use JSON.stringify when saving and JSON.parse when retreiving the data.诀窍是在保存时使用JSON.stringify并在检索数据时使用JSON.parse

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

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