我正在尝试使用“百分比编码”发表评论,但似乎一切正常,当 php 文件将值保存到 JSON 列表时,编码更改为“\u2764\ufe0f”。
为什么会这样?JSON不应该将数据索引为字符串吗?
迅速:
let emo = "%E2%9D%A4%EF%B8%8F"
emo.stringByRemovingPercentEncoding // ♥
Alamofire.request(
.POST,
"https://example.com/test.php?MyComment=\(emo)", parameters: ["":""])
PHP:
$results = array ( array(
"MyComment" => $MyComment
));
$inp = file_get_contents('Test.json');
$arr = json_decode($inp);
$results = array_merge($results, $arr);
$fp_login = fopen('Test.json', w);
fwrite($fp_login, json_encode($results));
fclose($fp_login);
echo $results // [{"MyComment":"\u2764\ufe0f"}]
应保存为 [{"MyComment":"%E2%9D%A4%EF%B8%8F"}]