我有一个简单的JSON文件:
{
"json.transactionDetails":{
"transactionDate":1266518024,
"transactionType":"bought",
"userDescription":"reimbursement",
"transactionDescription":"Bought 10 AAPL @200",
"quantity":10,
"amount":"2010.00",
"price":"200.00",
"commission":"10.00",
}
}
然后获取JSON的PHP文件:
$t_detail = file_get_contents("details.json");
$transaction_detail = json_decode($t_detail);
$tr_detail = $transaction_detail->json.transactionDetails; //this is my issue
$transaction_type = $tr_detail->transactionType;
$user_description = $tr_detail->userDescription;
如何解析“json.transactionDetails”?这段时间导致我的脚本失败。如果我删除“json”。从两个脚本一切正常。有没有办法跳过树中的第一个对象或什么?我无法改变输出JSON的方式,因为它来自外部API。
答案 0 :(得分:1)
尝试
$transaction_detail = json_decode($t_detail,true);
$tr_detail = $transaction_detail['json.transactionDetails'];
当你将json_decode
的第二个参数设为true时,它会将json解析为一个数组,这样你就可以使用array['key']
语法简单地获取值
或者你可以
$transaction_detail = json_decode($t_detail);
$tr_detail = $transaction_detail->{"json.transactionDetails"};