I’m new to JSON so having some trouble parsing it.
I made a JSON file:
newjson.json
{ "title":"My Title", "contents":"My\ Multiline\ Contents" }
I used \ to avoid an error in JavaScript syntax that doesn’t allow multiline strings, but when I load it with file_get_contents() and decode it using json_decode() in PHP, it can’t parse it.
I think I should handle the \s with preg_replace, or something before I put the string in a decode function.
What should I do?
PHP can’t parse your string because it’s not valid JSON. The only valid escape sequences are:
\"for a quotation mark\\for a backslash\/for a forward slash\bfor a backspace\ffor a formfeed\nfor a newline\rfor a carriage return\tfor a tab\uxxxxfor a hexadecimal escapeUse a newline escape instead if you want literal newlines. Otherwise, you’ll have to live with a less-pretty string.