2012-09-14 42 views
15

我有一個URL參數傳遞使用json_encode每個值就像如下:PHP獲取值

{ 
    "countryId":"84", 
    "productId":"1", 
    "status":"0", 
    "opId":"134" 
} 

我可以使用json_decode來解析每個值:

$json = array 
(
    'countryId' => $_GET['CountryId'], 
    'productId' => $_GET['ProductId'], 
    'status' => $_GET['ProductId'], 
    'opId'  => $_GET['OpId'] 
); 

echo json_encode($json); 

,因爲它是返回的結果進一步的數據處理?

謝謝。如果第二值是真的

+3

你試過了嗎? – JaredMcAteer

+1

...什麼?你沒有認真地嘗試在PHP中使用json_encode,然後在同一個PHP中json_decode呢?我很困惑。 – FrankieTheKneeMan

+3

他可能想將編碼的json存儲在某個地方以備將來使用 –

回答

44

json_decode()將返回一個對象或數組:

$json = '{"countryId":"84","productId":"1","status":"0","opId":"134"}'; 
$json = json_decode($json, true); 
echo $json['countryId']; 
echo $json['productId']; 
echo $json['status']; 
echo $json['opId']; 
+0

感謝兄弟,爲我工作:)非常簡單且不錯的解決方案 –

+0

我如何從這個'[{「countryId」:「84」,「productId」: 「1」,「status」:「0」,「opId」:「134」}]' – 151291

+0

@ 151291'$ json [0] .countryId' –

7

json_decode將返回原來編碼相同的陣列。 對於instanse,如果你

$array = json_decode($json, true); 
echo $array['countryId']; 

OR

$obj= json_decode($json); 

echo $obj->countryId; 

這些都將呼應84 我覺得json_encode和json_decode函數名是不言自明的...

+0

對不起,我的錯,編輯了答案 –