如何将Json转换为HTML表?

时间:2014-09-12 09:13:34

标签: php html json

我有一个输出一些json的脚本。

我的json看起来像这样:

{"_get_RoomResult":{"DATA":{"FIELD":[{"F0":"project_no","F1":"room_no"},"",{"F0":"BR1","F1":"A302"},{"F0":"KB2","F1":"1202"}]}}}

结构如下:(来自.asmx):

enter image description here

如何使用PHP将数据转换为HTML表格?

我尝试了以下内容:

<?php
                include("lib/nusoap.php");
                $client = new nusoap_client("http://originserv.homeip.net/pap_test2/service1.asmx?wsdl",true); 
                $params = array('KeyWord'=>'1234!@#$%^','Server_Name'=>'Server_Name','Database_Name'=>'PAP_Property','ProgramName'=>'Web_Cust_Room','Program_Module'=>'','Current_User'=>'','PcUser'=>'','PcName'=>'','IP'=>'','User_Version'=>'','Group_Name'=>'ARA','Username'=>'thitima_k');
                $data = $client->call('_get_Room', $params);
                //print_r($data);
                 $result=json_encode($data);
                //print_r($result);
                $mydata = json_decode($result,true); 
?>

接下来我该怎么办?

我希望输出显示如下:

enter image description here

1 个答案:

答案 0 :(得分:0)

如何将JSON数据转换为HTML表格?

<?php

// json
$result = '{"_get_RoomResult":{"DATA":{"FIELD":[{"F0":"project_no","F1":"room_no"},"",{"F0":"BR1","F1":"A302"},{"F0":"KB2","F1":"1202"}]}}}';

// turn json into array
$data = json_decode($result,true);

// render table
echo "<table>";

// the data to iterate over is in a sub-array
//var_dump($data['_get_RoomResult']['DATA']['FIELD']);

$render_header = true;

foreach($data['_get_RoomResult']['DATA']['FIELD'] as $values)
{
    if(!is_array($values)) continue; // json data contains a string, skip that

    if($render_header === true) {
        echo '<tr><th>' . $values['F0'] . '</th><th>' . $values['F1'] . '</th></tr>';
        $render_header = false; // render header only once
    } else {
        echo '<tr><td>' . $values['F0'] . '</td><td>' . $values['F1'] . '</td></tr>';
    }
}

echo "</table>";