带有 UTF8 字符的 PHP JSON

标签 php json

我正在使用此 PHP 发送 JSON 消息:

$result = mysql_query("select a.id, ad.nombre, a.imagen, a.lat, a.long, ad.desc, a.url, a.email, a.tel, a.direccion, a.cp, a.poblacion, a.provincia from `bck_alrededor` a, `bck_alrededor_description` ad, `bck_alrededor_partner` ap
where a.id = ad.id_alrededor
and a.id = ap.id_alrededor
and a.id_cat = '$cat'
and ad.language = '$idioma'
and ap.id_partner = '$idp'",$link);

    while( $row = mysql_fetch_array($result) )
    {   
        $id = $row['id'];
        $nombre = $row['nombre'];
        $imagen=$row['imagen'];
        $lat=$row['lat'];
        $long=$row['long'];
        $desc=$row['desc'];
        $url=$row['url'];
        $email=$row['email'];
        $tel=$row['tel'];
        $direccion=$row['direccion'];
        $cp=$row['cp'];
        $poblacion=$row['poblacion'];
        $provincia=$row['provincia'];

        if ($imagen <>'')
        {
            $imagen = $dir.'/'.$imagen;
        }

        $posts[] = array('nid'=> $id , 'title'=> $nombre, 'image'=> $imagen ,  'latitude'=> $lat, 'longitude'=> $long, 'html'=> $desc, 'web'=> $url, 'email'=> $email, 'phone'=> $tel, 'address'=> $direccion, 'cp'=> $cp, 'poblacion'=> $poblacion, 'provincia'=> $provincia );

    }
    $response['nodes'] = $posts;

    $current_charset = 'ISO-8859-15';
    array_walk_recursive($response,function(&$value) use ($current_charset){
         $value = iconv('UTF-8//TRANSLIT',$current_charset,$value);

    });

    echo json_encode($response);

    if(!headers_sent()) header('Content-Type: application/json; charset=utf-8', true,200);
    header('Content-type: application/json');

但是我收到了带有 UTF8 转义字符的 JSON 消息:

{"nodes":[{"nid":"87","title":"Tienda Oficial","image":"\/tiendaoficialgbc.png","latitude":"43.3021","longitude":"-1.9721","html":"Entra y adquiere todos los productos oficiales del GBC. En 48h los tienes en casa","web":"http:\/\/www.gipuzkoabasket.com\/tienda\/tienda_es.php","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b0c021b1e1100040a090a18000e1f2b0c021b1e1100040a090a18000e1f45080406" rel="noreferrer noopener nofollow">[email protected]</a>.","phone":"943 44 44 28","address":"Paseo de Anoeta 22, 1a Planta","cp":"20014","poblacion":"Donostia - San Sebasti\u00e1n","provincia":"Gipuzkoa"},{"nid":"88","title":"Tienda Oficial Salaberria","image":"\/tiendaoficialgbc.png","latitude":"43.30384","longitude":"-1.9797","html":"Entra y adquiere todos los productos oficiales del GBC. En 48h los tienes en casa","web":"http:\/\/www.gipuzkoabasket.com\/tienda\/tienda_es.php","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dabdb3aaafa0b1b5bbb8bba9b1bfae9abdb3aaafa0b1b5bbb8bba9b1bfaef4b9b5b7" rel="noreferrer noopener nofollow">[email protected]</a>.","phone":"943 44 44 28","address":"Jos\u00e9 Maria Salaberria 88","cp":"20014","poblacion":"Donostia - San Sebasti\u00e1n","provincia":""}]}

我尝试使用 echo json_encode(utf8_encode($response));但随后我在客户端应用程序中收到一条空 JSON 消息。

如何获取不带 UTF8 字符的常规 JSON 消息?

谢谢

最佳答案

\u00e1 是在 JSON 中转义 Unicode 字符的完全有效的方法。它是 JSON 规范的一部分。要将其解码为 UTF-8,只需 json_decode 即可。 utf8_decode 与此无关。

我不明白的是这段代码:

iconv('UTF-8//TRANSLIT',$current_charset,$value);

这表示您正在尝试从 UTF-8//TRANSLIT 转换为 ISO-8859-15,这没有多大意义。 //TRANSLIT 应该位于 ISO-8859-15 之后,否则您根本不应该进行此转换。

关于带有 UTF8 字符的 PHP JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10392667/

相关文章:

php - 未定义路由登录(多重身份验证)

php - Laravel Factory - 在单列中生成具有随机迭代量的假 json 数组数据

jquery - 以某种方式在 aspx 站点上显示 JSON 结果

java - 在 ViewPager 上跨多个 fragment 加载 Json,需要更好的方法

c# - JObject 解析返回额外的大括号

php如何进行部分搜索/输入和下拉菜单搜索?

php - 在 PHP 中使用缩进和格式化项目进行选择

php - 如何在wordpress选项中保存短代码的内容?

java - servlet 将空值添加到 JSON 请求

javascript - javascript中的json数组递归循环