解码PHP JSON

时间:2012-06-04 16:49:53

标签: php json

我的php收到类似于此的JSON字符串:

{"one-value":"GOOGLE","sub-values":{"sub thing":"xpto"}}

如何获得“子事物”的价值?

如果我想要“一个价值”:

$json = json_decode($data);
$service = $json->{'one-value'};

3 个答案:

答案 0 :(得分:1)

试试这个:

$json = json_decode(
  '{"one-value":"GOOGLE","sub-values":{"sub thing":"xpto"}}'
);

echo $json->{'sub-values'}->{'sub thing'};

// OR
$json = json_decode(
  '{"one-value":"GOOGLE","sub-values":{"sub thing":"xpto"}}',
  TRUE
);

echo $json['sub-values']['sub thing'];

答案 1 :(得分:0)

json_decode($data,true);   

将为您提供JSON中所有值的完整索引数组,之后您可以执行

$json["sub-values"]["sub thing"];

答案 2 :(得分:0)

$json = json_decode($data,true); 
$service = $json["sub-values"]['one-value'];

'true'用于将json字符串转换为关联数组