2013-03-14 88 views
1

我有以下php數組,並希望將其作爲乾淨的JSON文件進行打印,但我在括號和逗號周圍收到額外的引號。雖然我得到的當前JSON是有效的JSON,但它不是我想如何格式化的。見下文PHP數組,我json_encode輸出和首選JSON輸出:PHP數組json_encode添加額外引號

Array 
(
[0] =>    { 
[1] =>     code : "EU27", 
[2] =>     debtA : 967576248244, 
[3] =>     debtB : 6971817122583, 
[4] =>     id : "eur", 
[5] =>     name : "EU", 
[6] =>     popY : 8584663069945, 
[7] =>     popZ : 7565913300548   } 
[8] =>    , 
[9] =>    { 
[10] =>     code : "US", 
[11] =>     debtA : 9090198981283, 
[12] =>     debtB : 92189801202, 
[13] =>     id : "usa", 
[14] =>     name : "United States", 
[15] =>     popY : 4514703786570, 
[16] =>     popZ : 774697283542   } 
    ) 

當前json_encode輸出:

[ 
" {", 
" code : \"EU27\",", 
" debtA : 1739414366187,", 
" debtB : 8719158563660,", 
" id : \"eur\",", 
" name : \"EU\",", 
" popY : 460572909944,", 
" popZ : 2396933253407 }", 
" ,", 
" {", 
" code : \"US\",", 
" debtA : 7810766144794,", 
" debtB : 10536751929567,", 
" id : \"usa\",", 
" name : \"United States\",", 
" popY : 8571487476842,", 
" popZ : 1716024078740 }" 
    ] 

首選JSON輸出:

[ 
{ 
    code : "EU27", 
    debtA : 1739414366187, 
    debtB : 8719158563660, 
    id : "eur", 
    name : "EU", 
    popY : 460572909944, 
    popZ : 2396933253407 }, 
{ 
    code : "US", 
    debtA : 7810766144794, 
    debtB : 10536751929567, 
    id : "usa", 
    name : "United States", 
    popY : 8571487476842, 
    popZ : 1716024078740 } 
    ] 

我一直在尋找計算器,發現類似的問題,但不能得到它的工作。可能是因爲我對php和php數組的理解。任何幫助表示讚賞。

+0

爲什麼你想要不帶引號的屬性?那是無效的json – sroes 2013-03-14 08:21:36

+0

您是否創建了該數組?您應該爲此使用2維數組。 – Antony 2013-03-14 08:27:19

+0

只是爆破你的數組值。 – revoua 2013-03-14 08:28:11

回答

1

你在做語法不正確的數組的第一個:

[0] => {

其次,你正在尋找的結果是對象的數組。這說;你會想要做類似如下:

$array = array(); 

$array[0] = new StdObject; 
$array[0] -> code = "EU27"; 
// other values added to object 

$array[1] = new StdObject; 
$array[1] -> code = "US"; 
// other values added to second object 

$json = json_encode($array); 

或者你也可以使用,而不是一個對象數組:

$array = array(); 

$array[0] = array(
"code" => "EU27", 
// other values 
); 

$array[1] = array(
"code" => "US", 
// other values 
); 

$json = json_encode($array); 
0

您可以輕鬆地做到這一點。

在這裏相關的形式首次使用數組如何:

$a = [ 
[ 
'code'=>'EU27', 
'debtA' => 967576248244, .... etc 
], 
[ 
'code'=>'Something', 
'debtA' => 874376347, .... etc 
] 
]; 

再投內部數組對象

foreach($a as $k = $v)$a[$k] = (object)$v; 

然後JSON編碼它... 結束悲劇。
這裏是我的嘗試作爲一個樣本:

$a = [ 
[ 
'code'=>'EU27', 
'debtA' => 967576248244//, .... etc 
], 
[ 
'code'=>'Something', 
'debtA' => 874376347//, .... etc 
] 
]; 

foreach($a as $k => $v)$a[$k]=(object)$a[$k]; 
echo json_encode($a); 

這是我得到:

[{"code":"EU27","debtA":967576248244},{"code":"Something","debtA":874376347}] 

希望這有助於。

+0

JSON會引用字符串鍵ex:代碼將會是「code」。但是這對實施和任務並沒有什麼影響。 – Ihsan 2013-03-14 08:52:29

0

您得到的JSON字符串是100%有效的,並且完全對應於您構建的輸入數組。 json_encode()永遠不會嘗試讓你認爲的猜謎遊戲:如果你有一個包含{的字符串,PHP將產生一個包含{的字符串 - 它甚至不會考慮啓動對象的可能性!

顯然,你需要建立你的數組是這樣的:

$input = array(
    array(
     'code' => 'EU27', 
     'debtA' => '1739414366187', 
     'debtB' => '8719158563660', 
     'id' => 'eur', 
     'name' => 'EU', 
     'popY' => '460572909944', 
     'popZ' => '2396933253407', 
    ), 
    array(
     'code' => 'US', 
     'debtA' => '7810766144794', 
     'debtB' => '10536751929567', 
     'id' => 'usa', 
     'name' => 'United States', 
     'popY' => '8571487476842', 
     'popZ' => '1716024078740', 
    ), 
); 

$output = json_encode($input); 

此外,鍵名必須是quoted- it isn't an option,因爲它們都是字符串:

字符串是零序列或更多的Unicode字符,包裹在 雙引號,使用反斜槓轉義。一個字符被表示爲單個字符串的 。字符串非常類似於C或Java 字符串。

0

你要正確的JSON是

'    [ 
        { 
        "code" : "EU27", 
        "debtA" : "1739414366187", 
        "debtB" : "8719158563660", 
        "id" : "eur", 
        "name" : "EU", 
        "popY" : "460572909944", 
        "popZ" : "2396933253407" }, 
       { 
        "code" : "US", 
        "debtA" : "7810766144794", 
        "debtB" : "10536751929567", 
        "id" : "usa", 
        "name" : "United States", 
        "popY" : "8571487476842", 
        "popZ" : "1716024078740" } 
        ] 

此JSON相當於multidimenstional陣列應該是這樣的: `

  Array 
       (
        [0] => Array 
         (
          [code] => EU27 
          [debtA] => 1739414366187 
          [debtB] => 8719158563660 
          [id] => eur 
          [name] => EU 
          [popY] => 460572909944 
          [popZ] => 2396933253407 
        ) 

        [1] => Array 
         (
          [code] => US 
          [debtA] => 7810766144794 
          [debtB] => 10536751929567 
          [id] => usa 
          [name] => United States 
          [popY] => 8571487476842 
          [popZ] => 1716024078740 
        ) 

      )`   

爲了更好understaning看到這裏http://php.net/manual/en/function.json-encode.php'