开发者

how to format javascript array data that contains special characters

开发者 https://www.devze.com 2022-12-19 06:11 出处:网络
I\'m trying to construct a javascript array from a db query. Each item in the array is a string that can contain many different characters that mess up the array. Single quotes, double quot开发者_运维

I'm trying to construct a javascript array from a db query. Each item in the array is a string that can contain many different characters that mess up the array. Single quotes, double quot开发者_运维技巧es, parens, etc...

Here's an example of my current output:

var titleList = new Array('Fallout: New Vegas Teaser-Trailer HD','Saints NFC Champions','Best action scene of all time','NJ Lady ep 5: Our Grandma watches Jersey Shore','Australian Banker Caught Looking At Racy Images Of Model Miranda Kerr On Live Television','LEAKED FOOTAGE: New Griswold's "Vacation" Movie?','"A.D." teaser   (ZOMBIE ANIMATION)'

...and so on

Is there a special way i can encapsulate each array item so that the title's characters dont interfere with the JS?

Thanks for your help.


Yes, you can escape these characters. Read about JSON.

Example (Hebrew text in json serialized object):

{"updated_at":"2010/02/01 09:55:15 +0000",
 "title":"\u05d5\u05d9\u05ea\u05d5\u05e8"}


In general you need to escape the quoting characters you’re using for the string declaration. So in case of your string, you need to escape the ' inside your string declaration:

'LEAKED FOOTAGE: New Griswold\'s "Vacation" Movie?'

You either have to do the replacement manually (with some string replace function). Or maybe your language does support JSON functions. That would definitely be the better choice.


You can escape some special characters (double quote, single quote, newline, tab,..) with \ e.g.: var foo = "\" is just a double quote"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号