显示UNI code字符JSON [英] Displaying UNICODE characters in JSON

查看:174
本文介绍了显示UNI code字符JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示阿姆哈拉语字符,这是在的ListView 在我的Andr​​oid应用UTF-8字符,但什么是出现一系列问号???? ?这是 JSON 输出的样子:

I'm trying to display Amharic characters which are utf-8 character in a ListView in my android app, but what appears is series of question marks ????? This is what the json output looks like:

[{"ImageID":"1","ItemID":" Michael","ItemID_AM":" ???","ImagePath":"http:\/\/10.0.2.2
    \/android\/Pagination\/pics\/pic_a.png"},{"ImageID":"2","ItemID":" Mary","ItemID_AM":" 
    ????","ImagePath":"http:\/\/10.0.2.2\/android\/Pagination\/pics\/pic_b.png"},
    {"ImageID":"3","ItemID":"Sarah","ItemID_AM":"????","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_c.png"},
    {"ImageID":"4","ItemID":"John","ItemID_AM":"??","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_d.png"},
    {"ImageID":"5","ItemID":"Paul","ItemID_AM":"???","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_e.png"},
    {"ImageID":"6","ItemID":"Martha","ItemID_AM":"??????","ImagePath":"http:\/\/10.0.2.2
    \/android\/Pagination\/pics\/pic_f.png"},
    {"ImageID":"7","ItemID":"Abby","ItemID_AM":"??","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_a.png"},
    {"ImageID":"8","ItemID":"Bekei","ItemID_AM":"??","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_b.png"},
    {"ImageID":"9","ItemID":"Nani","ItemID_AM":"??","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_c.png"},
    {"ImageID":"10","ItemID":"Baby","ItemID_AM":"??","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_d.png"},
    {"ImageID":"11","ItemID":"Made","ItemID_AM":"???","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_e.png"},
    {"ImageID":"12","ItemID":"Fuche","ItemID_AM":"??","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_f.png"},{"ImageID":"13","ItemID":"Michael Fulle","ItemID_AM":"???
     ??","ImagePath":"http:\/\/10.0.2.2\/android\/Pagination\/pics\/pic_a.png"},
    {"ImageID":"14","ItemID":" Mary Assefa","ItemID_AM":"???? ???","ImagePath":"http:\/
    \/10.0.2.2\/android\/Pagination\/pics\/pic_b.png"},{"ImageID":"15","ItemID":"Sarah 
Michael","ItemID_AM":"???? ???","ImagePath":"http:\/\/10.0.2.2\/android\/Pagination\/pics\/pic_c.png"},{"ImageID":"16","ItemID":"John Michael","ItemID_AM":"?? 
    ???","ImagePath":"http:\/\/10.0.2.2\/android\/Pagination\/pics\/pic_d.png"},
    {"ImageID":"17","ItemID":"Paul Michael","ItemID_AM":"??? ???","ImagePath":"http:\/
    \/10.0.2.2\/android\/Pagination\/pics\/pic_e.png"},{"ImageID":"18","ItemID":"Martha 
    Ephrem","ItemID_AM":"?????? ????","ImagePath":"http:\/\/10.0.2.2\/android\/Pagination\/pics
    \/pic_f.png"},{"ImageID":"19","ItemID":"Item 19","ItemID_AM":"?? 19","ImagePath":"http:\/
    \/10.0.2.2\/android\/Pagination\/pics\/pic_h.png"},{"ImageID":"20","ItemID":"Item 
    20","ItemID_AM":"?? 20","ImagePath":""},{"ImageID":"21","ItemID":"Item 21","ItemID_AM":"??
     21","ImagePath":"http:\/\/10.0.2.2\/android\/Pagination\/pics\/pic_g.png"}]

ItemID_AM 是我的数据库中存储的阿姆哈拉语字符的字段名称,并在我的分贝它清楚地显示了阿姆哈拉语字符。这是语音UNI code。

ItemID_AM is a field name in my database which stores the Amharic characters, and in my db it clearly displays the Amharic characters. Which are Phonetic UNICODE.

下面是 MyDatabase的

    CREATE TABLE `images2` (
    `ImageID` int(2) NOT NULL auto_increment,
    `ItemID` varchar(50) NOT NULL,
    `ItemID_AM` varchar(50) NOT NULL,
    `ImagePath` varchar(50) NOT NULL,
    PRIMARY KEY (`ImageID`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=31 ;

    -- 
    -- Dumping data for table `images2`
    -- 

    INSERT INTO `images` VALUES (1, 'Michael', '???', 'http://10.0.2.2/android/Pagination/pics/pic_a.png');
    INSERT INTO `images` VALUES (2, 'Mary', '????', 'http://10.0.2.2/android/Pagination/pics/pic_b.png');
    INSERT INTO `images` VALUES (3, 'Sarah', '????', 'http://10.0.2.2/android/Pagination/pics/pic_c.png');
    INSERT INTO `images` VALUES (4, 'John', '??', 'http://10.0.2.2/android/Pagination/pics/pic_d.png');
    INSERT INTO `images` VALUES (5, 'Paul', '???', 'http://10.0.2.2/android/Pagination/pics/pic_e.png');
    INSERT INTO `images` VALUES (6, 'Martha', '??????', 'http://10.0.2.2/android/Pagination/pics/pic_f.png');

这是我的 getAllData.php

<?php
$host = "localhost"; // host of MySQL server
$user = "root"; // MySQL user
$pwd = ""; // MySQL user's password
$db = "mydatabase"; // database name

header('content-type: application/json; charset=utf-8');

// Create connection
$con = mysqli_connect($host, $user, $pwd, $db);

// Check connection
if(mysqli_connect_errno($con)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
} 

// query the application data
$sql = "SELECT * FROM images2 WHERE 1 ";
$result = mysqli_query($con, $sql);


// an array to save the application data
$rows = array();

// iterate to query result and add every rows into array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $rows[] = $row; 
}

// close the database connection
mysqli_close($con);


// echo the application data in json format
echo json_encode($rows);
?>

我已经用:标题(内容类型:应用程序/ JSON的;字符集= utf-8'); 但没有改变。可有人请F1,请,请?

I already used: header('content-type: application/json; charset=utf-8'); But nothing changed. Can someone F1 please, please, please?

推荐答案

您已经证实,<一个href=\"http://stackoverflow.com/questions/19182680/encoding-json-to-support-utf-8-characters-in-an-android-app#comment28462760_19182680\">Encoding JSON支持UTF-8字符在常规浏览器Android应用程序的是,你得到问号了。这表明,这个问题是服务器侧

You've already confirmed in Encoding JSON to support UTF-8 characters in an android app that in a regular browser, you get question marks too. This indicates that the problem is server side.

问题可能是,从PHP到MySQL数据库的连接没有被设置为UTF-8。期间的反应,任何非ISO8895-1字符将被转换为?。

The issue is probably that the database connection from PHP to MySQL is not set to UTF-8. During the response, any non-ISO8895-1 chars will be converted to "?".

设置字符与连接上设置:

Set the character set on the connection with:

mysqli_set_charset($con, "utf8")

在发出之前,选择

before you issue the select.

这篇关于显示UNI code字符JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆