2015-12-23 41 views
0

我是編程中的新手。我有一個任務,當我需要發送到美景的JSON作爲:在json中管理代碼和錯誤

{ 
    "code" : 200, 
    "error" : 0, 
    "data" : { 
     "sLogo" : "http://test.com/images/logo.png", 
     "aWinners": [ 
      { 
      "sName": "name" 
      } 
     ] 
    } 
} 

我現在該怎麼來管理數據,但我不明白如何管理codeerror。你能幫我提一些想法嗎? Thx提前,併爲我的英語感到抱歉。這個想法是,我需要發送到視圖這個JSON,但我不明白它包含什麼code

+0

能否請您闡述更多關於_'how管理代碼和你是否使用了框架error'_ –

+0

?在那種情況下,哪個框架? –

+0

不,我沒有使用指定的名氣,但這個項目是基於symfony –

回答

0

您可以使用json_decode()來獲取代碼和錯誤的值。

$json = '{"code" : 200,"error" : 0,"data" : {"sLogo" : "http://test.com/images/logo.png","aWinners": [{"sName": "name"}]}}'; 
$arr = json_decode($json, TRUE); 
echo $arr['code']; 
echo $arr['error']; 
+0

相同的組件。這個想法是,我需要發送這個JSON到視圖...但我不明白'code'必須包含 –

0
Note: Just pass the json as string from controller to your view and then decode your data in the view as per your requirement. 
In Controller 
<?php 
$json = '{"code" : 200,"error" : 0,"data" : {"sLogo" : "http://test.com/images/logo.png","aWinners": [{"sName": "name"}]}}'; 
?> 
In View 
<?php 
$arr = json_decode($json, TRUE); 
$code= $arr['code']; 
$error= $arr['error']; 
?>