#!/usr/bin/perl
use strict;
use warnings;
use JSON;

# Specify the path to your JSON file
my $json_file = 'json.dr';

# Read JSON data from the file
open my $json_fh, '<:encoding(UTF-8)', $json_file or die "Cannot open file: $!";
my $json_data = do { local $/; <$json_fh> };
close $json_fh;

# Parse JSON
my $data = decode_json($json_data);

# Generate HTML
my $html = generate_html($data);

# Specify the path to your output HTML file
my $output_file = 'index.html';  # Change the file name here

# Write HTML to the output file
open my $output_fh, '>:encoding(UTF-8)', $output_file or die "Cannot open file: $!";
print $output_fh $html;
close $output_fh;

# Function to generate HTML from JSON list of objects
sub generate_html {
    my ($data) = @_;

    my $html = <<'END_HTML';
<html lang="zh-cn">
<head>
  <meta charset="UTF-8">
  <title>JSON List to HTML</title>
</head>
<body>
END_HTML

    $html .= '<table border="1"><tr>';

    # Get the keys (column names) from the first object
    my @keys = sort keys %{ $data->[0] };

    # Create table header
    foreach my $key (@keys) {
        $html .= "<th>$key</th>";
    }

    $html .= '</tr>';

    # Create table rows
    foreach my $item (@$data) {
        $html .= '<tr>';
        foreach my $key (@keys) {
            my $value = $item->{$key};
            $html .= "<td>$value</td>";
        }
        $html .= '</tr>';
    }

    $html .= '</table>';
    $html .= '</body></html>';

    return $html;
}

