简体   繁体   中英

json_encode, utf8 and Greek characters

I am writing an android app and i want to query a db and get the results with json_encode. So i have this db:

CREATE DATABASE `my_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

I use utf8 because i want to store greek characters. All the tables have utf8_general_ci collation.

At last i have this php service that i use to query my db.

<?php
$host = "localhost";
$username = "root";
$password = "password";
$dbname = "my_db";

$con = mysql_connect($host,$username,$password) or die ('Error connecting to mysql');

mysql_query("SET character_set_results=utf8", $con);
mysql_query("SET NAMES 'utf8'", $con);
mysql_select_db($dbname,$con);

$query = "SELECT * from patients";
$result = mysql_query($query,$con);
while($e=mysql_fetch_assoc($result))
        $output[]=$e;

print(json_encode($output));
mysql_close($con);
?>

The problem is that in the output i dont get greek characters. Instead i get this:

[{"patient_id":"2","email":"patienta@mail.com","password":"password2","firstname":"\u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3","lastname":"\u039c\u03a0\u0391\u0396\u0399\u03a9\u03a4\u0397\u03a3","birth":"1989-11-11","mobile":"6941212121","tameio":"\u039f\u0393\u0391"}]

That is right. All the chars not in ascii will be converted into UTF-8 format.

The data will be decoded correctly by json_decode .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM