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:

"[&quot;Software&quot;,&quot;3rd Party&quot;]"

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
Add a ment  | 

2 Answers 2

Reset to default 7

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);

本文标签: javascriptConvert JSON Serialized String Containing HTML Entities into objectStack Overflow