使用 JSON 输出生成动态 html(使用 php)
我有多个具有不同结构的 JSON 文件。我想要做的是用 HTML 自动显示这些 JSON 输出。
我的一些 JSON 输出如下:(将每个文件视为单独的文件,需要单独处理)
{
"parts": [
{
"@attributes": {
"id": "part1"
},
"car": "Peugeot",
"service": 5,
"location": 2996,
"price": "44.95",
"date": "2000-10-01"
},
... other objects
]
}
{
"licenses":[
{
"driver":"John",
"year":26,
"info":null
},
... other objects
]
}
现在,为了处理这些文件,我在 PHP 上使用 GET 发送页面名称,并且我希望相应的 JSON 输出为使用 HTML 作为 $key 打印到屏幕上-> $value
如何使用 PHP 生成此动态 JSON 输出读取事件?我需要创建递归函数吗?
因为这些文件的结构彼此不同。我希望我能够解释我的问题。已经感谢您的帮助。
I have multiple JSON files with different structures. What I want to do is to automatically display these JSON outputs with HTML.
Some of my JSON outputs are as follows: (Think of each of these as separate files and need to be processed separately)
{
"parts": [
{
"@attributes": {
"id": "part1"
},
"car": "Peugeot",
"service": 5,
"location": 2996,
"price": "44.95",
"date": "2000-10-01"
},
... other objects
]
}
{
"licenses":[
{
"driver":"John",
"year":26,
"info":null
},
... other objects
]
}
Now, to process these files, I send the page name with GET on PHP and I want the corresponding JSON output to be printed to the screen with HTML as <span>$key</span> -> <span>$value</span>
How can I make this dynamic JSON output read event with PHP? Do I need to create a recursive function?
Because the files have different structures from each other. I hope I was able to explain my problem. Thanks already for yours help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议如下:
从 GET 或 POST 获取所需的 JSON 文件名,例如:
$jsonfilename = $_GET['文件'];
以上不包含任何安全保护!这是一个单独的主题,
所以做一些研究。
加载 json 文件并解析它:
$json = file_get_contents('/path/'.$jsonfilename);
$data = json_decode($json, true);
读取你的json数据:
foreach ($data as $key=>$value){
回显''.$key。' -> '.$值。'';
}
I suggest the following:
get required JSON file name from GET or POST, for example:
$jsonfilename = $_GET['file'];
The above does not include any security protection! this is a separate topic,
so do some research.
load your json file and parse it:
$json = file_get_contents('/path/'.$jsonfilename);
$data = json_decode($json, true);
read your json data:
foreach ($data as $key=>$value){
echo ''.$key.' -> '.$value.'';
}
您的 PARTS 文件的简单示例:
这将产生:
A simple example for your PARTS file:
This produces: