2013-05-12 127 views
2

我想写一个简单的PHP脚本女巫得到一个字符串,并在json中返回字符串。JSON PHP编码问题

根据这篇文章设置所有数据库和表格和html。 http://kunststube.net/frontback/

而且它的伟大工程:

  1. 的形式发送字符串与 俄语/日语/不管,
  2. 然后找到类似的数据库字符串writen文本到PHP。

但是。当我打印该字符串JSON:

$data = array('success'=> true,'message'=>$row['phrase']); 
echo json_encode($data); 

它返回到我这样的事情{"success":true,"message":"\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u043e\u0438\u0442"}

当我打印

echo $row['phrase']; 

它在俄罗斯/日本返回正常的字符串/不管

有什么建议吗?

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\ UPDATE \\\\\\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ////////////////////////

我有一个字段

在该字段为改变这个函数执行

$(document).ready(function(){ 

    $('#mike').bind('webkitspeechchange',function() 
    { 

     a= $(this).val(); 
     recognizeAjax(a); 

}) ; 
}); 


function recognizeAjax(string) { 

    var postData ="string="+string; 

    $.ajax({ 
     type: "POST", 
     dataType: "text", 
     data: postData, 
     beforeSend: function(x) { 
      if(x && x.overrideMimeType) { 
       x.overrideMimeType("application/json;charset=UTF-8"); 
      } 
     }, 
     url: 'restURL.php', 
     success: function(data) { 
      // 'data' is a JSON object which we can access directly. 
      // Evaluate the data.success member and do something appropriate... 
      if (data.success == true){ 
       alert(data.message); 
      } 
      else{ 
       alert(data.message+'11111'); 
      } 
     } 
    }); 


} 

这里是我的php

<?php header('Content-type: application/json; charset=utf-8'); 
error_reporting(E_ALL); 
ini_set('display_errors', true); 
// Here's the argument from the client. 
$string = $_POST['string']; 
$quest=1; 
//$quest=$_POST['quest']; 
//$event=$_POST['event']; 
//$success = processDomain($string); 

//!!!!!!!!!!!!! PLEASE SAY NOTHING ABOUT THE WAY I CONNECT it doesn't metter right now!!!! 
$con=mysql_connect("localhost", "*******", "*****") or die(mysql_error()); 
mysql_select_db("vocabulary", $con) or die(mysql_error()); 
mysql_set_charset('utf8', $con); 


$sql="SELECT * FROM `0` WHERE event_name = 'taxi' AND quest_id = '".$quest."'"; 

$result = mysql_query($sql); 


while($row = mysql_fetch_array($result)) 

{ 


    if ($string == htmlspecialchars($row['phrase'])) 
    { 
     // $sql="SELECT * FROM `1` WHERE step_id = '".$row['step_id']."' AND quest_id = '".$quest."'"; 

     // $result = mysql_query($sql); 
     // $answer = mysql_fetch_array($result); 
     // Set up associative array 

$data = array('success'=> true,'message'=>$row['phrase']); 
//echo $row['phrase']; 
// JSON encode and send back to the server 
echo json_encode($data); 
     break; 
    } else { 
// Set up associative array 
     $data = array('success'=> false,'message'=>'aint no sunshine'); 
     echo json_encode($data); 
     break; 
    } 
} 
mysql_close($con); 

但(根据JavaScript的)我总是收到undefied11111警报

所以我做了新的领域

<form method="post" id="wer" action="restURL.php" accept-charset="utf-8"> 
    <input name="www" style="position: relative;" lang="ru" /> 
    <input type="submit"> 
</form> 

,并返回我{ “成功”:真实的, “消息”:“\ u0441 \ u043a \ u043e \ u043b \ u044c \ u043a \ u043e \ u0441 \ u0442 \ u043e \ u0438 \ u0442“}当字符串合适时。

+2

这就是Unicode字符在JSON中的表示方式。 – Barmar 2013-05-12 18:26:08

+0

那么我如何才能在普通人物中看到它呢? – 2013-05-12 18:29:07

+0

我在我的php header('Content-Type:text/html; charset = utf-8'); – 2013-05-12 18:29:42

回答

1

在JSON中,\uXXXX是一种对unicode字符进行编码的方式,因此生成的字符串是ASCII安全的。

例如\u0441只是西里尔文的小写字母es。严格地说,你不需要编码这些字符,但它可能更安全。