phparraysjsondecodingstring-decoding

JSON decode string in PHP (array of arrays) Special characters


So I have this issue when converting a JSON string to a PHP array. The data is sent via HTTP POST so I'm aware there may be some decoding needed.

Could anyone lay an insight on how I would use json_decode() in PHP to convert this string to an array? "[[\"37\",\"text\",\"\\\"\\\"\"],[\"38\",\"text\",\"\\\"\\\"\"],[\"39\",\"text\",\"\\\"one word two words. Hello? \\\\\\\"escape\\\\\\\" lol\\\"\"]]"

The input was:

[
    ["37", "text", ""],
    ["38", "text", ""],
    ["39", "text", userInputtedString]
]

Where userInputtedString is: one word two words. Hello? "escape" lol

^ Or any other Unicode values


Solution

  • Use utf8_encode before json_decode

    $str = "[[\"37\",\"text\",\"\\\"\\\"\"],[\"38\",\"text\",\"\\\"\\\"\"],[\"39\",\"text\",\"\\\"one word two words. Hello? \\\\\\\"escape\\\\\\\" lol\\\"\"]]";
    $str = utf8_encode($str);
    $str = json_decode($str,JSON_UNESCAPED_SLASHES);