i got a result from someone else code
elem.getAttribute("additional-attributes")
...the result is like:
"{"k1":"v1", "k2":"v2"}"
so do I have to parse the string to get "v2" from "k2"? or are there better ways to get the value?
Thanks!
You could just split on comma then colon. Or a simple regex could do the trick.
If the string is like this actually (note the ' as a string operator):
var string = '{"k1":"v1", "k2":"v2"}'
You can do this (to convert that string into JS object):
var obj = eval("("+string+")");
And then to access k2
value do this:
var k2value = obj.k2;
If the string is badly formated, you can reformat it so it is valid JS string, and then do the eval() on it to make it an object.
OK,所以您想使用JSON.parse
获取对象,然后使用obj.k1
或obj.k2
获取值...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.