恩从PHP code任意字符以JSON数组和Android中使用它们 [英] Encode Arbitrary Characters to json array from php and use them in Android
本文介绍了恩从PHP code任意字符以JSON数组和Android中使用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使用JSON从数据库填充的ListView
。在的ListView
填充罚款,但字符用问号代替。我看着JSON。字符是在希腊。
I am trying to populate a listView
from database using JSON. The listView
populates fine but the characters are replaced with question marks. I looked at the JSON. The characters are in Greek.
我的PHP code:
My php code:
<?php
try {
$handler = new PDO('mysql:host=localhost;dbname=database', 'root', 'password');
$handler->query( 'SET CHARACTER SET utf8' );
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
echo $e->getMessage();
die();
}
$query = $handler->query( 'SET CHARACTER SET utf8' );
$query = $handler->query('SELECT * FROM markoglou_order');
$records = array();
$records = $query->fetchAll(PDO::FETCH_ASSOC);
$json['categories_markoglou'] = $records;
echo json_encode($json, JSON_UNESCAPED_UNICODE);
?>
截图:
编辑:
我已经尝试过使用 JSON_UNESCAPED_UNI code
,但没有奏效。
I already tried using JSON_UNESCAPED_UNICODE
but it didn't work.
推荐答案
执行以下操作:
- 更改数据库归类为utf8_general_ci,也可以使用相同的排序规则上,你需要的表
- 使用相同的排序规则在桌子上只(VARCHAR,文本) 领域
-
修改您的文件类似如下:
- Change your database Collation to utf8_general_ci and also use the same collation on the tables that you need
- Use the same collation on the fields of the tables only(varchar, text)
Modify your file like the following:
<?php
mb_http_output("utf-8");
header("Content-type:application/json; charset=utf-8");
try {
$handler = new PDO('mysql:host=localhost;dbname=database', 'root', 'password');
$handler->query( 'SET CHARACTER SET utf8' );
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
echo $e->getMessage();
die();
}
$query = $handler->query( 'SET CHARACTER SET utf8' );
$query = $handler->query('SELECT * FROM markoglou_order');
$records = array();
$records = $query->fetchAll(PDO::FETCH_ASSOC);
$json['categories_markoglou'] = $records;
echo json_encode($json, JSON_UNESCAPED_UNICODE);
?>
然后你将不得不在希腊语所需的输出。
And then you are going to have the desired output in Greek language.
希望它可以帮助!
这篇关于恩从PHP code任意字符以JSON数组和Android中使用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文