1
我已成功从此JSON页面检索到两个属性,但我在获取“年份”属性方面遇到了麻烦。我想问题是我缺少一些父元素。如果有人能指引我正确的方向,我会非常感激,谢谢。这是JSON结构的链接:http://api.edmunds.com/api/vehicle/v2/vins/WAUYP54B01N099265?fmt=json&api_key=XXX使用PHP从JSON获取年份属性
你可以看到API的结果this eval.in
下面是代码:
<?php
$datvin="";
$title="";
$posts="";
$mds="";
// copy file content into a string var
if (isset($_POST['datvin'])){
$datvin=trim($_POST['datvin']);
$json_file = file_get_contents('http://api.edmunds.com/api/vehicle/v2/vins/'.$datvin.'?fmt=json&api_key=XXX');
// convert the string to a json object
$jfo = json_decode($json_file);
// read the title value
$title = $jfo->make->name;
// copy the posts array to a php var
$posts = $jfo->make->id;
$mds= $jfo->years->year;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="container">
<div class="header">
</div><!-- header -->
<h1 class="main_title"><?php echo $title; ?></h1>
<div class="content">
<form method="post">
<input type="text" name="datvin"><br>
<input type="submit">
</form>
<ul class="ul_json clearfix">
<?php
echo "$title<br>";
echo "$posts<br>";
echo "$mds";
?>
</ul>
</div><!-- content -->
<div class="footer">
</div><!-- footer -->
</div><!-- container -->
</body>
</html>
那么它会很高兴看到使用var_dump($ jfo)的最终数组;看看它看起来像什么...... – TimBrownlaw
用户在他的OP中离开了API密钥,我已经看到了结构。 – Styphon
是的,我刚刚检查过,但我从来没有把它拆开,我刚刚做过。 :)很好找! – TimBrownlaw