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
4 Answers
Reset to default 3The 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
版权声明:本文标题:javascript - how to tell is a string is a stringify'd JSON object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743965270a2569771.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论