json_encode和国家字符

时间:2014-12-02 21:11:06

标签: php arrays json utf-8 iso-8859-1

我有这个代码。但是当我使用查询并且我的国家字符ÆØÅ没有显示时,在json数组中它只是说“NULL”。

我如何解码或改变以便我可以使用这些字符? 当我将内容字符集设置为iso-8859-1时,我获得了国家字符。但正如我发现json编码只使用utf-8编码。那么我怎样才能使用ÆØÅ并让它在json数组中工作?     

/*
 * Following code will get single utested details
 * A utested is identified by utested id (uid)
 */
header('Content-Type: charset=UTF-8');
// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '/dbconnect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET["uid"])) {
    $uid = $_GET['uid'];

    // get a utested from utesteds table

    $result = mysql_query("
SELECT p.uid
      , u.name
      , u.description
      , u.url
      , u.picurl
      , u.mapurl
      , p.price
      , ROUND(AVG(r.rating),1) rating
   FROM utested u
   JOIN price p
     ON p.uid = u.uid
   JOIN ( SELECT uid, MAX(pid) latest_price FROM price GROUP BY uid ) px
     ON px.uid = p.uid
    AND px.latest_price = p.pid
   JOIN rating r
     ON r.uid = u.uid
WHERE u.uid=$uid
ORDER BY u.uid DESC LIMIT 1
") or die(mysql_error());

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $utested = array();
$utested["uid"] = $result["uid"];
$utested["name"] = $result["name"];
$utested["description"] = $result["description"];
$utested["url"] = $result["url"];
$utested["picurl"] = $result["picurl"];
$utested["mapurl"] = $result["mapurl"];
$utested["price"] = $result["price"];
$utested["rating"] = $result["rating"];



            // success
            $response["success"] = 1;

            // user node
            $response["utested"] = array();

            array_push($response["utested"], $utested);
//print_r ($response);
            // echoing JSON response
            echo json_encode($response);
        } else {
            // no utested found
            $response["success"] = 0;
            $response["message"] = "No utested found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no utested found
        $response["success"] = 0;
        $response["message"] = "No utested found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

这是我完整的PHP文件。请尝试适应,以便我可以测试它。

0 个答案:

没有答案