Php 我的Json不是';不能正确解析
简单脚本获取一个json文件,去掉无关的头,并尝试访问单个键/值,但运气不佳。有人吗 代码 我的建议是:Php 我的Json不是';不能正确解析,php,json,Php,Json,简单脚本获取一个json文件,去掉无关的头,并尝试访问单个键/值,但运气不佳。有人吗 代码 我的建议是: {"target_record":{"target_id":"32feaf056b8c46e4a6f5500b6289cf59","name":"Francesca Grace","width":300.0,"active_flag":true,"tracking_rating":5,"reco_rating":""},"status":"success","result_code":"Su
{"target_record":{"target_id":"32feaf056b8c46e4a6f5500b6289cf59","name":"Francesca Grace","width":300.0,"active_flag":true,"tracking_rating":5,"reco_rating":""},"status":"success","result_code":"Success","transaction_id":"b427cb1f89544b4c85332ca0ad174848"}
修剪后的json的打印\r
:
一,
使用“$obj->{'target_id'}”
获取名称给我这个:简单的解决方案:在json_解码中使用“true”将对象转换为关联数组。然后通过$Array[$key][key]访问其中的项 试试这个:
$postURL = "http://case42.com/ogle/GetTarget.php?targetid=32feaf056b8c46e4a6f5500b6289cf59";
$json = file_get_contents($postURL);
echo "original response = <P>" . $json ."<HR>";
$strip = "target_record";
$mypos = strpos($json, $strip);
$json = substr($json,$mypos-2);
echo "My Trimmed json is: <P>" . $json."<HR>";
$obj = json_decode($json, true);
echo "<P>The print _ r of the trimmed json prints:<P> " . print_r ($json);
echo "<HR>";
echo "using \"\$obj->{'target_id'}\" to get name give me this: ".$obj['tagret_record']['name']."<HR>";
$postrl=”http://case42.com/ogle/GetTarget.php?targetid=32feaf056b8c46e4a6f5500b6289cf59";
$json=file\u get\u contents($postrl);
回显“原始响应=”$json“
”;
$strip=“target\u record”;
$mypos=strpos($json,$strip);
$json=substr($json,$mypos-2);
echo“我的json是:”$json“
”;
$obj=json_decode($json,true);
echo“经过修剪的json打印的打印:
”。打印(json);
回声“
”;
echo“使用\$obj->{'target\u id'}\”获取名称给我:“.$obj['tagret\u record']['name']”“
”;
。(原因是您忘记了修剪输出中的{“target\u record:
)。刚刚修复了此问题-相同的结果…请参阅。您是否尝试回显打印($array)
?如果是,这不是使用打印的方法。请阅读。“当返回参数为TRUE时,此函数将返回一个字符串。否则,返回值为TRUE。”因为当您使用,TRUE
,再次作为(“当为TRUE时,返回的对象将转换为关联数组”),它返回一个数组。在PHP中访问对象的方法是$obj->key
,但数组是$array”这是因为,它不是$obj['name']
,而是$obj['target\u record']['name']
。
{"target_record":{"target_id":"32feaf056b8c46e4a6f5500b6289cf59","name":"Francesca Grace","width":300.0,"active_flag":true,"tracking_rating":5,"reco_rating":""},"status":"success","result_code":"Success","transaction_id":"b427cb1f89544b4c85332ca0ad174848"}
$postURL = "http://case42.com/ogle/GetTarget.php?targetid=32feaf056b8c46e4a6f5500b6289cf59";
$json = file_get_contents($postURL);
echo "original response = <P>" . $json ."<HR>";
$strip = "target_record";
$mypos = strpos($json, $strip);
$json = substr($json,$mypos-2);
echo "My Trimmed json is: <P>" . $json."<HR>";
$obj = json_decode($json, true);
echo "<P>The print _ r of the trimmed json prints:<P> " . print_r ($json);
echo "<HR>";
echo "using \"\$obj->{'target_id'}\" to get name give me this: ".$obj['tagret_record']['name']."<HR>";