【发布时间】:2017-12-25 03:05:32
【问题描述】:
I got a JSON from a web site in the raw format:
{"A_B":{"id":7,"last":"0.00000038"},"A_C":{"id":8,"last":"0.00001938"}, ... }
我如何获得 A_B 和 A_C ???我不知道 A_B 和 A_C 可能是什么。
【问题讨论】:
I got a JSON from a web site in the raw format:
{"A_B":{"id":7,"last":"0.00000038"},"A_C":{"id":8,"last":"0.00001938"}, ... }
我如何获得 A_B 和 A_C ???我不知道 A_B 和 A_C 可能是什么。
【问题讨论】:
试试下面的。
$json = file_get_contents("your json data");
$arr = json_decode($json, true);
foreach ($arr as $key=>$val) {
var_dump($key);
}
最后,var_dump($key) 显示 A_B 和 A_C。
【讨论】:
假设您在某个变量中有原始 json,例如 $yourJson
首先,解析你的 json。 $parsedJson = json_decode($yourJson, true)
然后运行一个 foreach 循环并在 2 次迭代后停止,您将(希望)获得正确的键值对。
$i = 0;
$extractedValues = [];
foreach ($parsedJson as $key => $value) {
$extractedValues[$key] = $value; $i++;
if ($i === 2) {
break;
}
}
现在,$extractedValues 仅包含从前两次迭代中找到的两个元素。
【讨论】:
$data = json_decode("Your json variable");
foreach($data as $value){
echo $value; // here you receive your desire value.
}
【讨论】:
试试这个:
$json = '{"A_B":{"id":7,"last":"0.00000038"},"A_C":{"id":8,"last":"0.00001938"}}';
$dataArray = json_decode($json, true);
$arrayKeys = array_keys($dataArray); // in your case A_B and A_C
如果你想得到它们的值,那么:
foreach($dataArray as $data) {
foreach($data as $key => $value) {
echo $key . ": " . $value . PHP_EOL;
}
}
【讨论】:
var $jsonObj = json_decode('{"A_B":{"id":7,"last":"0.00000038"}}')
print $jsonObj->{'A_B'}
【讨论】: