-2

Inside an AJAX call, there is the following php code:

$jsonOutput =
                "{
                            id: \"0\",
                            name: \"dauerreservierung\",
                            startDate: new Date('".date("Y-m-d", strtotime('-100 year',
                            $today))."'),
                            endDate: new Date('".date("Y-m-d", strtotime('+20 year',
                            $today))."'),
                            \"color\": \"#FF0000\",
                        }";

I really have problems following this syntax. What are all those slashes doing there? Why are the " connected to the slashes? And: Is an associative array created here?

John Conde
  • 217,595
  • 99
  • 455
  • 496
ForeFather321
  • 179
  • 2
  • 9

3 Answers3

3

This variable is not an array, it's a string containing a JSON object.

In PHP, if you want to put quotes in a string, you have to escape them using \

Examples :

$my_life = 'I\'m eating an apple';
$json = "{\"id\": 1, \"value\": 42}";

You should try to echo $jsonOutput; to see what is happening

gogaz
  • 2,323
  • 2
  • 23
  • 31
1

The backslash (\) is a special character in both PHP and JSON. Both languages use it to escape special characters in strings and in order to represent a backslash correctly in strings you have to prepend another backslash to it, both in PHP and JSON.

For more details refer this PHP manual http://php.net/manual/en/language.types.string.php#language.types.string.syntax.single

and refer to this StackOverflow link as well.

0

if you are consuming this response in PHP, use

json_decode(input_string)

to get (backslashes) out of the json

If you are consuming in javascript, use

string.replace(/\\\//g, "/"); or JSON.parse(input_string)
Pramod Patil
  • 2,704
  • 2
  • 14
  • 20