admin管理员组

文章数量:1356876

For anyone interested, I ended up building a "localstorage with expirations" script here,

What I'm doing: building an extension for Storage, so that the user can do this:

localStorage.setThing(key, value)

and the user can do the following:

localStorage.setThing("key1", 1)
localStorage.setThing("key2", "this is a string")
localStorage.setThing("key3", { prop1: "this is a json obj" })

In my setThing method, I'm checking for the typeof for value, and if typeof value == "object", I'm storing it as localStorage.setItem(key, JSON.stringify(value))

On the getThing method, I know that the value that makes it into localStorage is always going to be a string. So, how can I do this?

var val = localStorage.getItem("key3")
if (val is a previously JSON.stringify'd object) // <-- ??
    return JSON.parse(val)

Do I need to do a regex check on val, and if so, does anyone have a pattern handy which tells me if a string is really a JSON.stringify'd object?

Thanks!

For anyone interested, I ended up building a "localstorage with expirations" script here, http://plugins.jquery./project/localcache

What I'm doing: building an extension for Storage, so that the user can do this:

localStorage.setThing(key, value)

and the user can do the following:

localStorage.setThing("key1", 1)
localStorage.setThing("key2", "this is a string")
localStorage.setThing("key3", { prop1: "this is a json obj" })

In my setThing method, I'm checking for the typeof for value, and if typeof value == "object", I'm storing it as localStorage.setItem(key, JSON.stringify(value))

On the getThing method, I know that the value that makes it into localStorage is always going to be a string. So, how can I do this?

var val = localStorage.getItem("key3")
if (val is a previously JSON.stringify'd object) // <-- ??
    return JSON.parse(val)

Do I need to do a regex check on val, and if so, does anyone have a pattern handy which tells me if a string is really a JSON.stringify'd object?

Thanks!

Share Improve this question edited Dec 27, 2011 at 14:55 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Nov 21, 2011 at 0:24 Ian DavisIan Davis 19.4k30 gold badges91 silver badges134 bronze badges 1
  • Try the EAFP - It's easier to ask for forgiveness than it is to get permission) - pattern [Grace Hopper], see also: en.wikipedia/wiki/Python_syntax_and_semantics#Exceptions. – miku Commented Nov 21, 2011 at 0:32
Add a ment  | 

4 Answers 4

Reset to default 3

The usual way to tell if a string is JSON is to run it through a JSON decoder. If it succeeds, it is JSON :-) No need for a regex here.

Another option is to store an object {isJsonObj:true, jsonObj: jsonObj}, and check the type is Object, and isJsonObj is true. If either of those are false, then it isn't a json obj (this means you don't have to parse). I realise this means you won't always have a string in there, but it makes it easy to test, and won't carry a large amount of overhead.

What kind of speed do you need when doing this? If it isn't a big issue, you could always just try parsing/decoding val, and if it fails, then it isn't a JSON object.

You want to check if variable is stringify right?

const obj = {"apple": 1, "banana": 2, "cat": 3}
const stringify_value = JSON.stringify(obj)

You can try lodash isString

import isString from "lodash/isString"

if(isString(stringify_value)) {
  // the value is stringify
}else{
  // the value is not stringify   
}

本文标签: javascripthow to tell is a string is a stringify39d JSON objectStack Overflow