localStorage:以不同方式存储对象与简单数据类型? [英] localStorage: Storing Objects vs Simple Data Types in different ways?

查看:147
本文介绍了localStorage:以不同方式存储对象与简单数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了使用 JSON.stringify和JSON.parse 来存储和检索HTML5 localStorage中的值/对象的方法。 它有效,但它整理所有内容 - 包括字符串,数字等。我想改进它。我想避免字符串化非对象的简单数据类型。我有一个方法,我用来确定参数是否是一个对象,如果它是一个对象,JSON.stringify它。

I've seen the approach of using JSON.stringify and JSON.parse to store and retrieve values/objects from HTML5 localStorage. It works, but it "stringifies" everything - including strings, numbers, etc. I'd like to improve it. I'd like to avoid "stringifying" simple data types that are not objects. I've got a method below that I use to determine if a parameter is an object and JSON.stringify it if it is an object.

lsSet: function(key, value) {

  ((Object.prototype.toString.call(value) === '[object Object]')) 
    ? localStorage.setItem(key,JSON.stringify(value)) 
    : localStorage.setItem(key,value);
  return (localStorage.getItem(key)) ? true : false;
},

是否有一种很好的方法可以识别您正在检索的localStorage字符串是一个字符串化的对象?我正在考虑检查字符串是否以{开头}结束,但似乎必须有更好的方法。下面是我当前的localStorage get方法,但在尝试JSON.parse简单数据类型(如字符串)时会抛出错误。

Is there a good way to identify if a localStorage string you are retrieving is a stringified object? I was thinking of checking if the string starts with { and ends with }, but it seems like there must be a better approach. Below is my current localStorage get method, but throws errors when trying to JSON.parse simple data types such as string.

lsGet: function(key) {
    return JSON.parse(localStorage.getItem(key));
},

典型的控制台错误是由于JSON.parse:

Typical console error is due to to the JSON.parse:

Uncaught SyntaxError: Unexpected token E 

任何想法?想到了一个想法,也许只是一个尝试/捕获?

Any ideas? A thought just came to mind, maybe just a try/catch?

var value;
try {
  value = JSON.parse(localStorage.getItem(key));
} catch(err) {
  value = localStorage.getItem(key);
}
return value;

编辑:注意,我正在处理很多不同的数据,我真的不想要不得不担心我正在设置/获取的数据类型。因此创建了标准lsSet / lsGet方法。在我的应用程序中,数据存储在PHP Session,JavaScript Objects和localStorage中 - 有点混合。

Note, I'm juggling a lot of different data around and I don't really want to have to worry about the typeof data I'm setting/getting. Thus stardard lsSet/lsGet methods were created. In my app, data is stored in PHP Session, JavaScript Objects and localStorage - a bit of a mix.

推荐答案

没有办法要知道您的检索字符串是序列化对象还是普通字符串,除非基于您自己的规则。字符串null34[1,2,3] 可能都是有效的字符串或有效的JSON,除非您对预期内容有所了解,否则无法确定。

There's no way to know whether a string your retrieve is intended to be a serialized object or a plain string, except based on your own rules. The strings "null", "34", "[1,2,3]" could all be valid strings or valid JSON, and unless you have an idea of expected content there is no way to be sure.

所以我建议:


  • 序列化和反序列化所有内容。除非你的频率非常高(在这种情况下可能是localStorage是错误的选择),这在性能上应该不会太差。

OR


  • 为序列化字符串添加自定义标题,例如 JSON:{...}。然后,当您检索字符串时,检查标题并相应地处理。

  • Add a custom "header" to your serialized strings, e.g. "JSON:{...}". Then, when you retrieve a string, check for the header and process accordingly.

这篇关于localStorage:以不同方式存储对象与简单数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆