[英]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? 如何将其转换为javascript中的对象?
I familiar with converting HTML Entities to DOM Objects: 我熟悉将HTML实体转换为DOM对象:
$("<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.
我不想创建HTML,我需要创建一个对象。
Use the built-in JSON.parse
: 使用内置的
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,因此,如果需要支持IE8之前的浏览器,则可以使用
$.parseJSON()
而不是JSON.parse()
。 (jQuery simply calls JSON.parse()
when it's available.) (jQuery可用时仅调用
JSON.parse()
。)
You can use "he" library with JSON.parse . 您可以将“ he”库与JSON.parse结合使用 。 "he" can encode and decode HTML code.
“他”可以编码和解码HTML代码。
var str = he.decode("["Software","3rd Party"]");
var obj = JSON.parse(str);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.