简体   繁体   English

json_encode,utf8和希腊字符

[英]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. 我正在编写一个Android应用程序,我想查询数据库并使用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. 我使用utf8是因为我想存储希腊字符。 All the tables have utf8_general_ci collation. 所有表都具有utf8_general_ci排序规则。

At last i have this php service that i use to query my db. 最后,我有用于查询数据库的php服务。

<?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. 所有不在ascii中的字符都将转换为UTF-8格式。

The data will be decoded correctly by json_decode . 数据将通过json_decode正确解码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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