Json_encode,json_decode和UTF8

Jak*_*ake 3 php json zend-framework

所有,

我使用PHP向Web服务器发出JSON请求,它在变量中返回一个JSON响应.JSON响应将包含许多键和值.我从服务器获得的JSON响应中包含特殊字符.因此,我使用以下语句将其转换为UTF8,解码JSON并将其用作数组以显示到UI.

$response = json_decode(utf8_encode($jsonresponse));
Run Code Online (Sandbox Code Playgroud)

现在,我必须在JSON请求中将相同的值传递给服务器以执行某些操作.但是,当我通过

$jsonrequest = json_encode(utf8_encode($request));
Run Code Online (Sandbox Code Playgroud)

到服务器,它失败了.

以下代码成功读取特殊字符并将其显示到UI.但如果我必须将utf8_encode值传递给服务器,则会失败.

当前的整个往返代码如下:

$requestdata  = json_encode($request);
$jsonresponse = //Do something to get from server;
$response = json_decode(utf8_encode($jsonresponse));
Run Code Online (Sandbox Code Playgroud)

我怎样才能修改它,以便传递确切的值来表示我从服务器的json响应中收到的内容?

Art*_*cto 7

我从服务器获得的JSON响应中包含特殊字符.因此,我使用以下语句将其转换为UTF8,解码JSON并将其用作数组以显示到UI.

JSON数据已经以UTF-8编码.你不应该再把它转换成UTF-8; 你会破坏数据.

而不是这个:

$response = json_decode(utf8_encode($jsonresponse));
Run Code Online (Sandbox Code Playgroud)

你应该这样:

$response = json_decode($jsonresponse); //already UTF-8!
Run Code Online (Sandbox Code Playgroud)


Chr*_*rra 2

您是否尝试过切换功能的位置?

$jsonrequest = utf8_encode(json_encode($request));
Run Code Online (Sandbox Code Playgroud)

utf8_encode 只编码字符串而不是数组