简体   繁体   中英

How to use double quoted string for parseJSON?

Below piece of code is working fine.

$(document).ready(function() {
    var jsonp = '[{"Lang":"jQuery","ID":"1"},{"Lang":"C#","ID":"2"}]';
    var lang = '';
    var obj = $.parseJSON(jsonp);
    $.each(obj, function() {
        lang += this['Lang'] + "<br/>";
    });
    $('span').html(lang);
});

For the string "jQuery", I need to use "jQu\\"something\\"ery" to become the text as JQu"something"ery. But it doesn't work. Is there any solution for this?

Thank you!

You need to make sure the quotes in the JSON are escaped, which means doing some double-escaping:

var jsonp = '[{"Lang":"jQu\\\"something\\\"ery","ID":"1"},{"Lang":"C#","ID":"2"}]';

That JavaScript string contains this JSON:

[{"Lang":"jQu\"something\"ery","ID":"1"},{"Lang":"C#","ID":"2"}]

As you can see, the string value for the Lang property contains escaped quotes.

Live Example | Source

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