tags:

views:

2394

answers:

2

I am generating the json from PHP.

+14  A: 

The correct way is to use json_encode()

Greg
Right, *you* wouldn't ever want to escape the text, you let the library do it
Gareth
For the Googling masses: json_escape() misses many important characters that will choke up a super-strict JSON validator (iPhone JSON libraries in my experience; errors not found by jslint.com or jsonlint.com). Examples are tabs and returns \t \r
editor
+1  A: 

The JSON is just a subset of Javascript, so everything you know about escaping strings in Javascript applies to JSON.

And yes, the correct answer is to use one of library functions, there is a quite a few of them around: see A completely fair and balanced comparison of php json libraries.

Stepan Stolyarov