Encoding HTML in JSON
Posted: Tue Nov 25, 2008 10:31 am
Hi Thar!
I'm in the middle of doing something and for testing purposes i want to hard code a variable in JSON and decode it with json_decode(). I'm having trouble encoding the JSON because it contains HTML tags and i just can't figure out how to make it work. Heres what i've tried with an example of a table containing one cell. Feel free to explain all the reasons why each one is syntactically incorrect, I'll do my best to explain what I think...
Ok well thats all i could think of. Any feedback is much appreciated cos I can't do it 
I'm in the middle of doing something and for testing purposes i want to hard code a variable in JSON and decode it with json_decode(). I'm having trouble encoding the JSON because it contains HTML tags and i just can't figure out how to make it work. Heres what i've tried with an example of a table containing one cell. Feel free to explain all the reasons why each one is syntactically incorrect, I'll do my best to explain what I think...
Code: Select all
// The single backslash means that the ones around html attribute values terminate the string when it is parsed
$JSON = "{\"table\": \"<tbody><tr><td colspan=\"2\" rowspan=\"2\"></td></td></tr></tbody>\"}";
// No idea why this one doesnt work, I figured the triple backslash would work as a double escape but that fails too
$JSON = "{\"table\": \"<tbody><tr><td colspan=\\\"2\\\" rowspan=\\\"2\\\"></td></td></tr></tbody>\"}";
// I guess this fails because json_decode only likes strings defined with double quotes not single ones
$JSON = '{\"table\": \"<tbody><tr><td colspan=\\\"2\\\" rowspan=\\\"2\\\"></td></td></tr></tbody>\"}';