JSON编码使我从Ajax为空但不是从正常的PHP字符串 - php

大家好,这是php中的字符串(如果很长,抱歉)

$JsonSpecificheTitoli='{"titoli":{"TitoloEbay-1-Virtuemart":" Maglia Shirt Adidas Milan tg S M L Per sport d è numerico maniche corte Home ","TitoloEbay-2-EbayIT":" Maglia Shirt Adidas Milan tg M L Ibrahimovic Per sport d è numerico Home ","TitoloEbay-5-EbayIT":" Maglia Shirt Adidas Milan tg XL Per sport d è numerico maniche corte Home ","TitoloEbay-3-EbayDE":" Trikot Shirt Adidas Milan tg S M XL F�r Sport ist numerisch S/S Saison 09 10 ","TitoloEbay-6-EbayDE":" Trikot Shirt Adidas Milan tg L F�r Sport ist numerisch S/S Saison 09 10 Home ","TitoloEbay-4-EbayUK":" Jersey Shirt Adidas Milan tg L XL XXL For sports is numerically Kurze armel "},"specifiche":{"Virtuemart":{"":"Arrivo in 24/48 h con SDA oppure Possibilita di necessita di 4/5 giorni per la spedizione","Maniche":" maniche corte "},"EbayIT":{"":"Arrivo in 24/48 h con SDA oppure Possibilita di necessita di 4/5 giorni per la spedizione","Maniche":" maniche corte "},"EbayDE":{"":"Ankunft in 24/48 h M�glichkeit der SDA oder erfordert 4 / 5 Tage Versandkosten","Arme":" S/S "},"EbayUK":{"":"Arrival in 24/48 h Possibility of SDA or requires 4 / 5 days for shipping","Sleeves":" Kurze armel "}}}';

如果我直接用php从此字符串中创建一个json_decode没问题:

$pr = json_decode($JsonSpecificheTitoli);  //-->OK

但是如果我从REQUEST(来自Ajax请求)中获取它,它将给我null

$Tt = $_REQUEST['Titoli'];
$TtJ = json_decode ($Tt); // --> it says null

$v=json_last_error();  //--> gives me 5 , is Syntax error? 

无论如何,奇怪的是,如何在请求中复制和打印相同的字符串会使它变坏?

谢谢

参考方案

$v=json_last_error();给我5,语法错误吗?

您可以像JSON_ERROR_SYNTAX一样针对the error constants进行检查,只需回显值即可:

echo JSON_ERROR_SYNTAX; # 4
echo JSON_ERROR_UTF8;   # 5

因此,在您的情况下,这是UTF8错误。 json_decode­Docs将UTF-8编码的字符串作为输入。只要确保它是UTF-8编码的就可以了。

$_REQUEST(和类似的输入变量)的编码取决于发送到您的应用程序中的HTTP请求(输入)的编码,该请求通常取决于原始网站的编码/字符集。

无法可靠地检测到它,因此最好了解它。但是,如果需要检测mb_http_input­Docs功能,它将很有帮助。

获取PHP脚本的HTTP请求中使用哪种编码的信息,然后在将传入数据传递到json_decode之前,根据需要对传入数据进行重新编码。您可以借助iconv function­Docs或Multibyte String functions­Docs在PHP中重新编码字符串。

此外,您还可以验证输入是否有效的有效UTF-8字符串,否则,请拒绝接受对您的应用程序的无效请求。要检查字符串是否为UTF-8,可以对字符串运行正则表达式:

$isUTF8 = preg_match('//u', $string);

(另请参见How to detect malformed utf-8 string in PHP?)

PHP-MySQL结果转换为JSON - php

我试图了解如何将MySQL结果转换为JSON格式,以便以后可以在Javascript中使用此JSON来构建HTML表。但是我的代码只是产生大量的空值,我还不明白为什么。$result = mysqli_query($con, "SELECT * FROM Customers"); $test = json_encode($result);…

json_encode网址失败 - php

有人在this bug附近吗?echo json_encode(array('url'=>'/foo/bar')); {"url":"\/foo\/bar"} 我使用Zend_Json and Zend_Json_Expr以便我甚至可以在js对象中获取回调函数-但我无法获得…

json_encode的特定方式 - php

所以这是我的PHP<?php include 'connect.php'; $sql = "SELECT name from startup_schema"; $result = mysqli_query($mysqli, $sql) or die("Error in Selecting " …

PHP:获取调用引用的数组名称 - php

假定以下函数并调用:function doSomething( &$someArray ) { // Do something to $someArray } $names=array("John", "Paul", "George", "Ringo"); doSomet…

PHP:将数组值加在一起 - php

我相信这比标题听起来要难一些,但我可能完全错了。我有一个像这样的数组:[["londrina",15],["cascavel",34],["londrina",23],['tiradentes',34],['tiradentes',21]] 我希望能够采用通用…