0

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

Lewis Smallwood
  • 84
  • 1
  • 11
  • Is that _actually_ what you're getting in the request? If so, something in the bit that's doing the posting appears to have gone a bit screwy. – Jonnix Jun 07 '16 at 11:37
  • Or you have an addslashes somewhere, or potentially magic quotes turned on, or both. etc – Jonnix Jun 07 '16 at 11:39
  • @JonStirling Yeah, it's just coming from Javascript as an AJAX parameter. Just with the standard `JSON.stringify()` on the array in javascript. It's so strange... – Lewis Smallwood Jun 07 '16 at 11:41

3 Answers3

2

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);
DsRaj
  • 2,288
  • 1
  • 16
  • 26
1

What seems to be the problem?

Simply use json_decode like you mentioned.

$ans = json_decode($_POST["name-of-var"]);

This should do it.

Swakeert Jain
  • 776
  • 5
  • 16
1

You could also use uft8_encode (to send to HTML) and uft8_decode (to receive) but not the right way

Hiren Makwana
  • 1,976
  • 2
  • 13
  • 28