2

I'm sending a JSON message using this PHP:

$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');

But I've got this JSON message with UTF8 escaped characters:

{"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":""}]}

I've tried to use echo json_encode(utf8_encode($response)); but then I got a null JSON message in the client app.

How can I get a regular JSON message without UTF8 characters?

Thanks

mram888
5,1275 gold badges36 silver badges59 bronze badges
asked May 1, 2012 at 1:58

1 Answer 1

2

\u00e1 is a perfectly valid way to escape Unicode characters in JSON. It's part of the JSON spec. To decode that to UTF-8, just json_decode it. utf8_decode has nothing to do with it.

What I don't understand is this code:

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

This says you're trying to convert from UTF-8//TRANSLIT to ISO-8859-15, which doesn't make much sense. The //TRANSLIT should come after ISO-8859-15, or you shouldn't be doing this conversion at all.

answered May 1, 2012 at 2:05
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your indications. It's very strange but my client app JSON parser is giving invalid json message and I can't access address value.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.