python构建json_如何使用Python构建JSON API

python构建jsonThe JSON API specification is a powerful way for enabling communication between client and server. It specifies the structure of the requests and responses sent between the two, using the ...
摘要由CSDN通过智能技术生成

python构建json

The JSON API specification is a powerful way for enabling communication between client and server. It specifies the structure of the requests and responses sent between the two, using the JSON format.

JSON API规范是启用客户端和服务器之间通信的强大方法。 它使用JSON格式指定在两者之间发送的请求和响应的结构。

As a data format, JSON has the advantages of being lightweight and readable. This makes it very easy to work with quickly and productively. The specification is designed to minimise the number of requests and the amount of data that needs sending between client and server.

JSON作为一种数据格式,具有轻巧易读的优点。 这使得快速高效地工作变得非常容易。 该规范旨在最大程度地减少请求数量以及需要在客户端和服务器之间发送的数据量。

Here, you can learn how to create a basic JSON API using Python and Flask. Then, the rest of the article will show you how to try out some of the features the JSON API specification has to offer.

在这里,您可以学习如何使用Python和Flask创建基本的JSON API。 然后,本文的其余部分将向您展示如何尝试JSON API规范必须提供的某些功能。

Flask is a Python library that provides a 'micro-framework' for web development. It is great for rapid development as it comes with a simple-yet-extensible core functionality.

Flask是一个Python库 ,为Web开发提供了“微框架”。 它具有简单但可扩展的核心功能,因此对于快速开发非常有用。

A really basic example of how to send a JSON-like response using Flask is shown below:

下面显示了一个如何使用Flask发送类似于JSON的响应的非常基本的示例:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def example():
   return '{"name":"Bob"}'

if __name__ == '__main__':
    app.run()

This article will use two add-ons for Flask:

本文将为Flask使用两个附加组件:

大图 (The big picture)

The end goal is to create an API that allows client-side interaction with an underlying database. There will be a couple of layers between the database and the client - a data abstraction layer and a resource manager layer.

最终目标是创建一个API,该API允许客户端与基础数据库进行交互。 数据库和客户端之间将有几层-数据抽象层和资源管理器层。

Here's an overview of the steps involved:

以下是有关步骤的概述:

  1. Define a database using Flask-SQLAlchemy

    使用Flask-SQLAlchemy定义数据库
  2. Create a data abstraction with Marshmallow-JSONAPI

    使用Marshmallow-JSONAPI创建数据抽象

  3. Create resource managers with Flask-REST-JSONAPI

    使用Flask-REST-JSONAPI创建资源管理器
  4. Create URL endpoints and start the server with Flask

    创建URL端点并使用Flask启动服务器

This example will use a simple schema describing modern artists and their relationships to different artworks.

本示例将使用一个简单的模式来描述现代艺术家及其与不同艺术品的关系。

安装一切 (Install everything)

Before getting started, you'll need to set up the project. This involves creating a workspace and virtual environment, installing the modules required, and creating the main Python and database files for the project.

在开始之前,您需要设置项目。 这涉及创建工作区和虚拟环境,安装所需的模块以及为项目创建主要的Python和数据库文件。

From the command line create a new directory and navigate inside.

从命令行创建一个新目录并在其中导航。

$ mkdir flask-jsonapi-demo
$ cd flask-jsonapi-demo/

It is good practice to create virtual environments for each of your Python projects. You can skip this step, but it is strongly recommended.

为每个Python项目创建虚拟环境是一个好习惯。 您可以跳过此步骤,但强烈建议您这样做。

$ python -m venv .venv
$ source .venv/bin/activate

Once your virtual environment has been created and activated, you can install the modules needed for this project.

创建并激活虚拟环境后,即可安装该项目所需的模块。

$ pip install flask-rest-jsonapi flask-sqlalchemy

Everything you'll need will be installed as the requirements for these two extensions. This includes Flask itself, and SQLAlchemy.

您需要的所有内容都将安装为这两个扩展的要求。 这包括Flask本身和SQLAlchemy。

The next step is to create a Python file and database for the project.

下一步是为项目创建Python文件和数据库。

$ touch application.py artists.db

创建数据库架构 (Create the database schema)

Here, you will start modifying application.py to define and create the da

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值