Home > Backend Development > PHP Tutorial > Introduction to the method of uploading json files in PHP (code example)

Introduction to the method of uploading json files in PHP (code example)

不言
Release: 2023-04-04 21:14:01
forward
3557 people have browsed it

This article brings you an introduction to the method of uploading json files in PHP (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

HTTP: A hypertext transmission protocol. It is a standard protocol for computer-to-computer communication. It is now generally used for end-to-end communication.

1. Agreed content

  • Request/response message format

  • Request method GET/POST

  • Response status 200/404/302/304

  • Default request/response header

The header function in PHP is used to set the response header

Introduction to the method of uploading json files in PHP (code example)

<?php  
header(&#39;content-type:text/html&#39;);
?>
Copy after login

Supplement:

<?php  
header(&#39;Location:01.php&#39;);
?>
Copy after login

The client browser will automatically Jump to the specified address

JSON
JSON: A means of expressing data similar to js literals

  1. The attribute name in JSON must be Use double quotes

  2. Strings in JSON must use double quotes (js strings can use single quotes)

  3. JSON does not allow comments

JSON data type
null:

 null
Copy after login

string:

"ssq"
Copy after login

boolean:

ture

number:

 12
Copy after login

object:

 {
    "name": "ssq",
    "age": 12,
    "gender": ture,
    "boyfrind": null
}
Copy after login

array:

 ["张三", "李四", "王五"]
Copy after login

JSON basic format

var obj = [
    {"name": "ss", "age": 12, "email": "ssss", "url": "sssss.com", "images": ["./images/01.jpg"]},
    {"name": "ss", "age": 12, "email": "ssss", "url": "sssss.com", "images": ["./images/01.jpg"]},
    {"name": "ss", "age": 12, "email": "ssss", "url": "sssss.com", "images": ["./images/01.jpg"]},
    {"name": "ss", "age": 12, "email": "ssss", "url": "sssss.com", "images": ["./images/01.jpg"]},
    {"name": "ss", "age": 12, "email": "ssss", "url": "sssss.com", "images": ["./images/01.jpg"]}
]
Copy after login

JSON conversion

Introduction to the method of uploading json files in PHP (code example)

Deserialize JSON in PHP

<?php
$contents = file_get_contents(&#39;storage.json&#39;);
$data = json_decode($contents, true);
?>
Copy after login

and convert it into the form of object array in PHP
Introduction to the method of uploading json files in PHP (code example)

01Example display

<?php

// 获取文件中记录的数据,并展示到表格中(动态生成表格的HTML标签)
$contents = file_get_contents(&#39;storage.json&#39;);
// $contents => JSON 格式的字符串
// 把 JSON 格式的字符串转换为对象的过程叫做反序列化

// json_decode 默认反序列化时 将 JSON 中的对象转换为 PHP 中 stdClass 类型的对象
$data = json_decode($contents, true);
// $data => []

?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>音乐列表</title>
  <link rel="stylesheet" href="bootstrap.css">
</head>
<body>
  <div class="container py-5">
    <h1>音乐列表</h1>
    <hr>
    <div>
      <a href="add.php" class="btn btn-secondary btn-sm">添加</a>
    </div>
    <table class="table table-bordered table-striped table-hover">
      <thead>
        <tr>
          <th>标题</th>
          <th>歌手</th>
          <th>海报</th>
          <th>音乐</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($data as $item): ?>
        <tr>
          <td><?php echo $item[&#39;title&#39;] ?></td>
          <td><?php echo $item[&#39;artist&#39;] ?></td>
          <td><img  src="<?php echo $item[&#39;images[0]&#39;] ? alt="Introduction to the method of uploading json files in PHP (code example)" >" alt=""></td>
          <td><audio src="<?php echo $item[&#39;source&#39;] ?>" controls></audio></td>
          <td><button class="btn btn-danger btn-sm">删除</button></td>
        </tr>
        <?php endforeach ?>
      </tbody>
    </table>
  </div>
</body>
</html>
Copy after login

Rendering

Introduction to the method of uploading json files in PHP (code example)

The above is the detailed content of Introduction to the method of uploading json files in PHP (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template