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

3
  • 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. Commented Jun 7, 2016 at 11:37
  • Or you have an addslashes somewhere, or potentially magic quotes turned on, or both. etc Commented Jun 7, 2016 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... Commented Jun 7, 2016 at 11:41

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This kinda solved it, thank you. I had to perform the utf8 encode in Javascript before passing the input through a POST
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.

2 Comments

It just returns NULL. Sorry, I should have been more specific
Ah interesting, it looks like my POST input isn't sanitised. Thank you!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.