Tip : Problem with single and double quotes in JSON

Sometimes if your javascipt code is not working and if you are using JSON in there, may be you want to check use double and single quotes in json string.

For example check following code :


$.ajax({
url:'-----',
data:"{'username': 'Ganesh','text': 'This is test'}",
})

This code may not work because you have single quotes inside and double outside 😦 . I know this is totally something you might unexpect, but you can expect unexpected from javascript 😉

Following will work.


$.ajax({
url:'-----',
data:'{"username": "Ganesh","text": "This is test"}'
})

Best option would be create a JSON object separately and use JSON.stringify


var obj = {'username': 'Ganesh','text': 'This is test'};
$.ajax({
url:'-----',
data:JSON.stringify(obj)
})

Happy javascripting 🙂

Advertisements

2 thoughts on “Tip : Problem with single and double quotes in JSON

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s