UTF-8中的值在JSON中编码为NULL [英] Values in UTF-8 being encoded as NULL in JSON

查看:140
本文介绍了UTF-8中的值在JSON中编码为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组关键字,这些关键字是通过JSON从数据库(编码的UTF-8)通过JSON传递的,其中一些可能具有特殊字符,例如é,è,ç等.完成者.示例:

I have a set of keywords that are passed through via JSON from a DB (encoded UTF-8), some of which may have special characters like é, è, ç, etc. This is used as part of an auto-completer. Example:

array('Coffee', 'Cappuccino', 'Café');

我应该添加来自数据库的数组为:

I should add that the array as it comes from the DB would be:

array('Coffee', 'Cappuccino', 'Café');

但是JSON编码为:

["coffee", "cappuccino", null];

如果我通过print_r()打印这些文件,它们会在UTF-8编码的网页上很好地显示,但是如果我想使用print_r()查看数组,则咖啡馆使用café"(如果使用文本/纯文本) $ array); exit();.

If I print these via print_r(), they show up fine on a UTF-8 encoded webpage, but café comes through as "café" if text/plain is used if I want to look at the array using print_r($array);exit();.

如果我在编码为JSON之前使用utf8_encode()进行编码,就可以了,但是在网页上显示的是café"而不是café".

If I encode using utf8_encode() before encoding to JSON, it comes through fine, but what gets printed on the webpage is "café" and not "café".

也很奇怪,但是json_last_error()被视为未定义函数,但是json_decode()和json_encode()可以正常工作.

Also strange, but json_last_error() is being seen as an undefined function, but json_decode() and json_encode() work fine.

关于如何从数据库中获取UTF-8编码数据以在整个过程中表现相同的任何想法吗?

Any ideas on how to get UTF-8 encoded data from the database to behave the same throughout the entire process?

EIDT:这是PHP函数,可捕获关键字并将它们分成一个数组:

EIDT: Here is the PHP function that grabs the keywords and makes them into a single array:

private function get_keywords() 
{
    global $db, $json;

    $output = array();

    $db->query("SELECT keywords FROM listings");

    while ($r = $db->get_array())
    {
        $split = explode(",", $r['keywords']);

        foreach ($split as $s)
        {
            $s = trim($s);
            if ($s != "" && !in_array($s, $output)) $output[] = strtolower($s);
        }
    }

    $json->echo_json($output);
}

json :: echo_json方法仅进行编码,设置标题并打印(供Prototype使用)

The json::echo_json method just encodes, sets the header and prints it (for usage with Prototype)

数据库连接方法:

function connect()
{

    if ($this->set['sql_connect'])
    {
        $this->connection = @mysql_connect( $this->set['sql_host'], $this->set['sql_user'], $this->set['sql_pass'])
                OR $this->debug( "Connection Error", mysql_errno() .": ". mysql_error());
        $this->db = @mysql_select_db( $this->set['sql_name'], $this->connection)
                OR $this->debug( "Database Error", "Cannot Select Database '". $this->set['sql_name'] ."'");

        $this->is_connected = TRUE;
    }

    return TRUE;
}

更多更新: 我运行的简单PHP脚本:

More Updates: Simple PHP script I ran:

echo json_encode( array("Café") ); // ["Caf\u00e9"]
echo json_encode( array("Café") ); // null

推荐答案

原因可能是当前客户端字符设置.一个简单的解决方案是将客户端设置为 mysql_query('SET CHARACTER SET utf8') 在运行SELECT查询之前.

The reason could be the current client character setting. A simple solution could be to do set the client with mysql_query('SET CHARACTER SET utf8') before running the SELECT query.

更新(2014年6月)

从PHP 5.5.0开始不推荐使用mysql扩展.现在建议使用mysqli.另外,在进一步阅读后,应避免上述设置客户端设置的方法出于安全性原因.

The mysql extension is deprecated as of PHP 5.5.0. It is now recommended to use mysqli. Also, upon further reading - the above way of setting the client set should be avoided for reasons including security.

我还没有测试过,但这应该可以替代:

I haven't tested it, but this should be an ok substitute:

$mysqli = new mysqli("localhost", "my_user", "my_password", "my_db");
if (!$mysqli->set_charset('utf8')) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
    printf("Current character set: %s\n", $mysqli->character_set_name());
}

或带有连接参数:

$conn = mysqli_connect("localhost", "my_user", "my_password", "my_db");
if (!mysqli_set_charset($conn, "utf8")) {
    # TODO - Error: Unable to set the character set
    exit;
}

这篇关于UTF-8中的值在JSON中编码为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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