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
答案 0 :(得分:1)
什么似乎是问题?
只需像你提到的那样使用json_decode。
$ans = json_decode($_POST["name-of-var"]);
这应该这样做。
答案 1 :(得分:1)
你也可以使用uft8_encode(发送到HTML)和uft8_decode(接收)但不是正确的方式
答案 2 :(得分:1)
在json_decode之前使用utf8_encode
$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);