2013-02-08 84 views
0

我在一個亞馬遜EC2實例上使用PHP 5.3.15(cli)(built:Jul 20 2012 23:10:20)。當我調用json_encode()時,生成的JSON會隨機跳過字符,以使JSON不再有效。即使在使用完全相同的參數/數據進行呼叫時,也只會在某些時間發生。奇怪的是,它發生在不同的地方,隨機點,沒有模式。它不會發生在相同的數據和相同的代碼,並在我的本地MAMP安裝相同版本的PHPphp json_encode()輸出隨機跳過字符

...「updateTime」:「2012-12-21 03:24:14」, 「createTime」:「2012-12-21:」21「,」updateTime「:」2012-12-25 19:33:25「,...

你可以看到上面的效果在21 :「21」我甚至不知道從哪裏開始解決這個問題

我已經重新啓動,甚至創建的服務器的新虛擬實例

我。非常需要幫助。任何你可以建議的將是真棒!

+1

Mmh,你的輸入數據看起來像什麼? –

+0

如果你發佈你的意見,我們可能會找出一些東西。但我瘋狂的猜測是,如果你的輸入是一個assoc數組,你會在21後錯過逗號。 – Redian

回答

1

必須設置JSON編碼參數QUOT或其他

$json=json_encode($array,JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE); 

這是JSON編碼是php.net

<?php 
    $a = array('<foo>',"'bar'",'"baz"','&blong&', "\xc3\xa9"); 

    echo "Normal: ", json_encode($a), "\n"; 
    echo "Tags: ", json_encode($a, JSON_HEX_TAG), "\n"; 
    echo "Apos: ", json_encode($a, JSON_HEX_APOS), "\n"; 
    echo "Quot: ", json_encode($a, JSON_HEX_QUOT), "\n"; 
    echo "Amp: ",  json_encode($a, JSON_HEX_AMP), "\n"; 
    echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n"; 
    echo "All: ",  json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT |JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n"; 

    $b = array(); 

    echo "Empty array output as array: ", json_encode($b), "\n"; 
    echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n"; 

    $c = array(array(1,2,3)); 

    echo "Non-associative array output as array: ", json_encode($c), "\n"; 
    echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "\n\n"; 

    $d = array('foo' => 'bar', 'baz' => 'long'); 

    echo "Associative array always output as object: ", json_encode($d), "\n"; 
    echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n"; 
?> 

輸出的一個示例:

Normal: ["<foo>","'bar'","\"baz\"","&blong&","\u00e9"] 
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"] 
Apos: ["<foo>","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"] 
Quot: ["<foo>","'bar'","\u0022baz\u0022","&blong&","\u00e9"] 
Amp: ["<foo>","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"] 
Unicode: ["<foo>","'bar'","\"baz\"","&blong&","é"] 
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"] 

Empty array output as array: [] 
Empty array output as object: {} 

Non-associative array output as array: [[1,2,3]] 
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}} 

Associative array always output as object: {"foo":"bar","baz":"long"} 
Associative array always output as object: {"foo":"bar","baz":"long"} 
0

我使用代理來分析發生了什麼。我一個接一個地提出了兩個相同的請求。注意到這些請求可能是Objective-C並且它可能不是被破解的php,而只是第二個請求取消第一個請求。