簡體   English   中英

PHP的json_decode返回空字符串(非NULL)

[英]PHP's json_decode returns empty string (not NULL)

我正在通過AJAX從客戶端向服務器發送字符串化的JSON。 但是,即使PHP對我來說似乎結構正確,也無法對其進行解碼。 我一直在尋找類似問題的答案,在大多數問題中, json_decode返回NULL (在這里不是這種情況),而是返回一個空字符串。

JavaScript發送的內容:

{"columns":{"l1":{"name":"Impressions"},"l2":{"name":"Clicks"}},"domain":"Media Performance"}

PHP輸出的內容(帶有轉義引號的內容):

{\"columns\":{\"l1\":{\"name\":\"Impressions\"},\"l2\":{\"name\":\"Clicks\"}},\"domain\":\"Media Performance\"}

有人可以在這里看到問題嗎? 看起來很簡單,但是json_decode不斷返回一個空字符串。

對於未發布代碼,我深表歉意。

這是AJAX( str是上面給出的JS字符串):

$.ajax({
            type: "POST",
            url: base + "handlers/createreport.php",
            data: {
                str: str
            }
        })

這是createreport.php ,沒有任何處理:

$str = $_POST['str'];

echo $str;  //prints out the string given above with escaped quotes
echo json_decode($str);     //empty string

服務器將AJAX中data作為POST參數接收,因此您無需解碼json,否則可以像數組一樣使用它。

我只是對其進行測試,並且可以正常工作: var_dump($_POST['str']);

<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b>
  'columns' <font color='#888a85'>=&gt;</font> 
    <b>array</b>
      'l1' <font color='#888a85'>=&gt;</font> 
        <b>array</b>
          'name' <font color='#888a85'>=&gt;</font>  <small>string</small> <font color='#cc0000'>'Impressions'</font> <i>(length=11)</i>
  'l2' <font color='#888a85'>=&gt;</font> 
    <b>array</b>
      'name' <font color='#888a85'>=&gt;</font> <small>string</small>     <font color='#cc0000'>'Clicks'</font> <i>(length=6)</i>
  'domain' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'Media Performance'</font> <i>(length=17)</i>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM