2012-05-01 115 views
2

我使用发送JSON消息这PHPPHP JSON与UTF8字符

$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":"[email protected]","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":"[email protected]","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消息?

感谢

回答

2

\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之后,否则你不应该进行这种转换。

+0

感谢您的指示。这很奇怪,但我的客户端应用程序JSON解析器给无效的json消息,我无法访问地址值。 – theomen