使用 JSON 输出生成动态 html(使用 php)

发布于 2025-01-13 12:51:43 字数 736 浏览 0 评论 0原文

我有多个具有不同结构的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风透绣罗衣 2025-01-20 12:51:43

我建议如下:

  1. 从 GET 或 POST 获取所需的 JSON 文件名,例如:

    $jsonfilename = $_GET['文件'];

以上不包含任何安全保护!这是一个单独的主题,
所以做一些研究。

  1. 加载 json 文件并解析它:

    $json = file_get_contents('/path/'.$jsonfilename);

    $data = json_decode($json, true);

  2. 读取你的json数据:

    foreach ($data as $key=>$value){
    回显''.$key。' -> '.$值。'';
    }

I suggest the following:

  1. 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.

  1. load your json file and parse it:

    $json = file_get_contents('/path/'.$jsonfilename);

    $data = json_decode($json, true);

  2. read your json data:

    foreach ($data as $key=>$value){
    echo ''.$key.' -> '.$value.'';
    }

策马西风 2025-01-20 12:51:43

您的 PARTS 文件的简单示例:

$json = '
{
    "parts":
    [
      {
        "@attributes": {
          "id": "part1"
        },
        "car": "Peugeot",
        "service": 5,
        "location": 2996,
        "price": "44.95",
        "date": "2000-10-01"
      },
      {
        "@attributes": {
          "id": "part2"
        },
        "car": "Renault",
        "service": 8,
        "location": 3100,
        "price": "99.95",
        "date": "2022-03-01"
      }
    ]
}';

$arr = json_decode($json, true);

foreach($arr["parts"] as $part) {
    foreach($part as $k => $v){
        if($k == "@attributes")
            echo "<h1>" . $v["id"] ."</h1>";
        else
            echo "<span>$k</span> -> <span>$v</span> <br/>";
    }
}

这将产生:

在此处输入图像描述

A simple example for your PARTS file:

$json = '
{
    "parts":
    [
      {
        "@attributes": {
          "id": "part1"
        },
        "car": "Peugeot",
        "service": 5,
        "location": 2996,
        "price": "44.95",
        "date": "2000-10-01"
      },
      {
        "@attributes": {
          "id": "part2"
        },
        "car": "Renault",
        "service": 8,
        "location": 3100,
        "price": "99.95",
        "date": "2022-03-01"
      }
    ]
}';

$arr = json_decode($json, true);

foreach($arr["parts"] as $part) {
    foreach($part as $k => $v){
        if($k == "@attributes")
            echo "<h1>" . $v["id"] ."</h1>";
        else
            echo "<span>$k</span> -> <span>$v</span> <br/>";
    }
}

This produces:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文