将Json数据逐个显示到php文件中

时间:2015-12-15 06:45:40

标签: php json

我有像这样的JSON数据

{
"no":"11",
"colorname":"Silver",
"colorgroupname":"MEDIUM SILVER"
}

我如何能够像这样显示到php文件

<div>
   <label>No :</label> 11
</div>
<div>
   <label>Color Name :</label> Silver
</div>
<div>
   <label>Color Group Name:</label> MEDIUM SILVER
</div>

3 个答案:

答案 0 :(得分:3)

请试试这个

<?php
$json = '{"no":"11","colorname":"Silver","colorgroupname":"MEDIUM SILVER"}';
$arr= json_decode($json,true);
$str="";
foreach($arr as $key => $val){
$str.="<div><label>$key :</label> $val</div>";
}
echo $str;
?>

答案 1 :(得分:0)

试试这个:

<?php
$data=json_decode('{"no":"11","colorname":"Silver","colorgroupname":"MEDIUM SILVER"}');
?>
<div>
   <label>No :</label> <?php echo $data->no; ?>
</div>
<div>
   <label>Color Name :</label> <?php echo $data->colorname; ?>
</div>
<div>
   <label>Color Group Name:</label> <?php echo $data->colorgroupname; ?>
</div>

答案 2 :(得分:-1)

<?php
    $str = '{
    "no":"11",
    "colorname":"Silver",
    "colorgroupname":"MEDIUM SILVER"
    }';
    $jsObj = json_decode($str);
    ?>

<div>
   <label>No :</label> <?php echo $jsObj->no;?>
</div>
<div>
   <label>Color Name :</label> <?php echo $jsObj->colorname;?>
</div>
<div>
   <label>Color Group Name:</label> <?php echo $jsObj->colorgroupname;?>
</div>