我的 php 接收到类似于以下的 JSON 字符串:
{"one-value":"GOOGLE","sub-values":{"sub thing":"xpto"}}
如何获得“子事物”的价值?
如果我想要“单值”:
$json = json_decode($data);
$service = $json->{'one-value'};
尝试这个:
$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'];
json_decode($data,true);
将为您提供 JSON 上所有值的完整索引数组,之后您可以执行
$json["sub-values"]["sub thing"];
$json = json_decode($data,true);
$service = $json["sub-values"]['one-value'];
'true' 用于将 json 字符串转换为关联数组