我在$ jsontest上使用json_decode(出于演示目的,因为它是确切的格式)。
问题是我正在尝试为附件7分配文件名,名称和类型,以将变量分隔为字符串。我尝试过
$attachment7filename = $jsontest2->{'attachment7->filename'};
例如,输出为PHP注意:
试图获取非对象的属性。
任何帮助将不胜感激。
<?php
$jsontest = '{"attachment7":{"filename":"small.flv","name":"small.flv","type":"video/x-flv"},"attachment2":{"filename":"sample.png","name":"sample.png","type":"image/png"},"attachment6": {"filename":"small.3gp","name":"small.3gp","type":"video/3gpp"},"attachment5": {"filename":"small.webm","name":"small.webm","type":"video/webm"},"attachment3": {"filename":"small.mp4","name":"small.mp4","type":"video/mp4"},"attachment1": {"filename":"109-1new.jpeg","name":"109- 1new.jpeg","type":"image/jpeg"},"attachment4": {"filename":"small.ogv","name":"small.ogv","type":"video/ogg"}}';
$jsontest2 = json_decode($jsontest, true);
$attachment7filename = $jsontest2->{'attachment7->filename'}; // should be small.flv for example
$attachment7name = $jsontest2->{'attachment7->name'};
$attachment7type = $jsontest2->{'attachment7->type'};
var_dump($attachment7filename);
?>
答案 0 :(得分:1)
您正在使用带有{}
引用的大括号(object
),例如$jsontest2->{'attachment7->filename'};
用{'attachment7->filename'}
替换['attachment7']['filename'];
这是代码
<?php
$jsontest = '{"attachment7":{"filename":"small.flv","name":"small.flv","type":"video/x-flv"},"attachment2":{"filename":"sample.png","name":"sample.png","type":"image/png"},"attachment6": {"filename":"small.3gp","name":"small.3gp","type":"video/3gpp"},"attachment5": {"filename":"small.webm","name":"small.webm","type":"video/webm"},"attachment3": {"filename":"small.mp4","name":"small.mp4","type":"video/mp4"},"attachment1": {"filename":"109-1new.jpeg","name":"109- 1new.jpeg","type":"image/jpeg"},"attachment4": {"filename":"small.ogv","name":"small.ogv","type":"video/ogg"}}';
$jsontest2 = json_decode($jsontest, true);
//print_r($jsontest2);
$attachment7filename = $jsontest2['attachment7']['filename']; // should be small.flv for example
$attachment7name = $jsontest2['attachment7']['name'];
$attachment7type = $jsontest2['attachment7']['type'];
var_dump($attachment7filename);
?>
您可以检查所需的output here