-1

我的 php 接收到类似于以下的 JSON 字符串:

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

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

如果我想要“单值”:

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

3 回答 3

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'];
于 2012-06-04T16:52:42.700 回答
0
json_decode($data,true);   

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

$json["sub-values"]["sub thing"];
于 2012-06-04T16:52:09.517 回答
0
$json = json_decode($data,true); 
$service = $json["sub-values"]['one-value'];

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

于 2012-06-04T16:52:18.893 回答