admin管理员组文章数量:1287959
I have a string that looks like this:
"["Software","3rd Party"]"
How can I convert this to an object in javascript?
I familiar with converting HTML Entities to DOM Objects:
$("<div/>").html(encodedStr).text();
My situation is a little different than the one above. I don't want to create HTML, I need to create an object.
I have a string that looks like this:
"["Software","3rd Party"]"
How can I convert this to an object in javascript?
I familiar with converting HTML Entities to DOM Objects:
$("<div/>").html(encodedStr).text();
My situation is a little different than the one above. I don't want to create HTML, I need to create an object.
Share Improve this question edited Jul 30, 2015 at 21:11 AnotherDeveloper asked Jul 30, 2015 at 20:47 AnotherDeveloperAnotherDeveloper 1,2722 gold badges15 silver badges38 bronze badges 1- 1 You have an HTML-escaped JSON string? – Dave Newton Commented Jul 30, 2015 at 20:50
2 Answers
Reset to default 7Use the built-in JSON.parse
:
var jstr = $("<div/>").html(encodedStr).text();
var obj = JSON.parse(jstr);
Since you're using jQuery anyway, you can use $.parseJSON()
instead of JSON.parse()
if you need to support browsers older than IE8. (jQuery simply calls JSON.parse()
when it's available.)
You can use "he" library with JSON.parse. "he" can encode and decode HTML code.
var str = he.decode("["Software","3rd Party"]");
var obj = JSON.parse(str);
本文标签: javascriptConvert JSON Serialized String Containing HTML Entities into objectStack Overflow
版权声明:本文标题:javascript - Convert JSON Serialized String Containing HTML Entities into object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741331076a2372767.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论