2012-09-14 44 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

对不起,我的错,编辑了答案 –