简体   繁体   中英

Convert JSON Serialized String Containing HTML Entities into 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.

Use 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("[&quot;Software&quot;,&quot;3rd Party&quot;]");
var obj = JSON.parse(str);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM