• 请不要在回答技术问题时复制粘贴 AI 生成的内容
IMZQZ
V2EX  ›  程序员

如何将一堆 json 文件变成 API 用于接口访问?

  •  
  •   IMZQZ · Jan 6, 2025 · 4265 views
    This topic created in 670 days ago, the information mentioned may be changed or developed.

    我想到的方法,各位看看对不对: 1.先根据 json 内容进行建表 2.将 json 导入数据库 3.写后端代码 4.写接口

    那么问题来了,我不会写代码,怎么办? 有没有什么学习路径或者有 AI 工具帮助做这个项目?

    22 replies  •  2025-01-08 08:15:19 +08:00
    hutoer
    hutoer
    Jan 6, 2025
    1
    试试 json-server
    txx
    txx
    Jan 6, 2025
    2
    json to graphql ?
    OneLiteCore
    OneLiteCore
    Jan 6, 2025
    3
    最近刚好做过类似的东西,简单说下:

    1. 用 FastAPI 和 Python 搭建简单的脚手架,用户以 JSONArray 的形式传入 文件名

    2. 接口接收到文件名后从某个硬盘目录上去查找对应的 "文件名.json" 文件

    3. Python 可以直接用 import json 包来解析文件成为 dict ,这一步也算是用来验证 json 的格式是否合法或者做一些校验

    4. 将 dict 转化成 string 作为 body 和 200 状态码返回就行了

    5. 用 docker 和 python 的镜像打包成 .tar 文件发送到你的云服务器

    6. 云服务器 sudo docker load -i XXXX.tar 镜像,然后用 docker compose 配置端口映射到本地的 8000 并启动

    7. 用 Nginx 配置当访问 /api/ 路径的时候,倒向本机的 8000 端口
    TimG
    TimG
    Jan 6, 2025 via Android
    4
    用 Python 的 fast-api 吧,入门很简单。代码多少得会写一点,不然错了都不好排查。
    OneLiteCore
    OneLiteCore
    Jan 6, 2025
    5
    整个流程还要配置域名、防火墙、http 自带加密和后台校验、json 文件和日志的定期备份啥的。我是自己要开发一个应用的后台所以需要自己处理这一整套的东西。
    OneLiteCore
    OneLiteCore
    Jan 6, 2025
    6
    但是如果你不需要逻辑,只是要纯粹可以让客户端可以访问到服务器上的 json 数据的话,我个人觉得最简单的办法就是直接用 Nginx 管理静态的 json 文件数据。甚至数据不敏感而且并不怎么重要的话,你直接扔 gitee 或者 github 上然后有个 Raw 按钮,点击就可以直接下载了。
    janda
    janda
    Jan 6, 2025❤️2
    7
    丢 oss 、直链请求
    PhDLuffy
    PhDLuffy
    Jan 6, 2025
    8
    放 github ,直连
    ntedshen
    ntedshen
    Jan 7, 2025
    9
    说真的。。。哥们你顶着这个头像问怎么 json 转 api 我还以为是什么数据库在打广告才点进来看的。。。
    结果你是真的不会写?
    likeman
    likeman
    Jan 7, 2025
    11
    不需要数据处理的话,直接丢 nginx ,然后请求链接
    maocat
    maocat
    Jan 7, 2025 via Android❤️1
    12
    啊,有这么麻烦吗,去到目录里面执行
    python -m http.server 不就好了
    junan
    junan
    Jan 7, 2025
    13
    随便找一个在线 mock 的网站
    kiracyan
    kiracyan
    Jan 7, 2025
    14
    直接 python 建个文件访问 直接读取
    IMZQZ
    IMZQZ
    OP
    Jan 7, 2025
    15
    谢谢楼上各位
    下面是一个我的示例 json
    {
    "status": {
    "code": 200,
    "messages": [
    "Success."
    ]
    },
    "data": [
    {
    "articleId": 0,
    "articleSub": 0,
    "freightGroupId": 83900,
    "issueDate": "0001-01-01T00:00:00",
    "effectiveDate": "0001-01-01T00:00:00",
    "hazard": false,
    "articleNotes": [],
    "articles": [
    {
    "articleId": 83980,
    "articleSub": 0,
    "freightGroupId": 83900,
    "class": "100",
    "description": "Billiard or Pocket Billiard (Pool) Tables, with other than stone or slate slabs (beds), KD, or with legs folded to underside of table top, see Notes, items 83982 and 83984, in boxes, crates or Package 1317",
    "issueDate": "0001-01-01T00:00:00Z",
    "effectiveDate": "0001-01-01T00:00:00Z",
    "hazard": false,
    "articleNotes": [
    {
    "noteReferenceId": 83982,
    "text": "NOTE-With each table there may be included not more than four cues and the equipment necessary for playing."
    }
    ],
    "articles": []
    },
    {
    "articleId": 83995,
    "articleSub": 0,
    "freightGroupId": 83900,
    "class": "77.5",
    "description": "Bubble Forming Compounds, plastic, see Note, item 83996, in boxes",
    "issueDate": "0001-01-01T00:00:00Z",
    "effectiveDate": "0001-01-01T00:00:00Z",
    "hazard": false,
    "articleNotes": [
    {
    "noteReferenceId": 83996,
    "text": "NOTE-One blowing tube may be included for each inner container."
    }
    ],
    "articles": []
    },
    {
    "articleId": 84060,
    "articleSub": 0,
    "freightGroupId": 83900,
    "class": "85",
    "description": "Dart Games, consisting of steel tipped darts and targets, in boxes",
    "issueDate": "0001-01-01T00:00:00Z",
    "effectiveDate": "0001-01-01T00:00:00Z",
    "hazard": false,
    "articleNotes": [],
    "articles": []
    }
    ]
    }
    ]
    }
    IMZQZ
    IMZQZ
    OP
    Jan 7, 2025
    16
    @ashin 谢谢 我去研究下
    IMZQZ
    IMZQZ
    OP
    Jan 7, 2025
    17
    @OneLiteCore 昨天我也看了 fastAPI 还在尝试中
    IMZQZ
    IMZQZ
    OP
    Jan 7, 2025
    18
    @OneLiteCore 不需要那么复杂 其实就是为了让别人通过关键词 搜索所有的 json 文件 然后将关键词相关的内容 返回就行
    vZexc0m
    vZexc0m
    Jan 7, 2025
    19
    直接放对象存储上得了。
    caoyang5689
    caoyang5689
    Jan 7, 2025
    20
    @likeman 确实, 不需要数据处理的话用这个就行, 划分数据的话可以拆分文件.
    llsquaer
    llsquaer
    Jan 7, 2025
    21
    你这个又不需要逻辑的,就是返回 json 文件。直接 Nginx 完事

    ```
    server {
    listen 80; # 监听 80 端口
    server_name yourdomain.com; # 替换为你的域名,没有就删除这行
    root /var/www; # 文件的目录

    # 配置 /json/ 路径下的请求处理
    location /json/ {
    default_type application/json; # 设置响应的内容类型为 JSON
    }

    # 其他 location 块和配置...
    }
    ```
    IMZQZ
    IMZQZ
    OP
    Jan 8, 2025
    22
    但是我搜索 description 里的内容的时候,返回需要返回这个 description 所在的集合的所有其他字段
    这样也可以做吗

    {
    "articleId": 83995,
    "articleSub": 0,
    "freightGroupId": 83900,
    "class": "77.5",
    "description": "Bubble Forming Compounds, plastic, see Note, item 83996, in boxes",
    "issueDate": "0001-01-01T00:00:00Z",
    "effectiveDate": "0001-01-01T00:00:00Z",
    "hazard": false,
    "articleNotes": [
    {
    "noteReferenceId": 83996,
    "text": "NOTE-One blowing tube may be included for each inner container."
    }
    }
    @llsquaer
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1515 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 04:16 · PVG 12:16 · LAX 20:16 · JFK 23:16
    ♥ Do have faith in what you're doing.