JSONPlaceholder

免费的在线REST API,为前端开发者提供模拟数据服务

api.example.com/posts/1
{
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

关于 JSONPlaceholder

一个为前端开发者提供的免费在线REST API服务,让开发和测试变得更加简单

名字含义

"JSONPlaceholder" 这个名字结合了两部分:JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,而Placeholder(占位符)表示它提供的是用于开发和测试的模拟数据,而不是真实的生产数据。

起源

JSONPlaceholder 由 Typicode 团队创建,首次发布于2014年。它的诞生源于开发者在前端开发过程中对模拟API的需求,使得前端开发者可以在后端API尚未准备好的情况下进行开发和测试。

为什么选择 JSONPlaceholder?

完全免费,无需注册或API密钥

即开即用,无需复杂配置

支持标准的RESTful操作

提供多种资源类型:帖子、评论、用户等

支持CORS,可在任何前端项目中使用

功能与服务

JSONPlaceholder 提供了丰富的模拟API端点,满足各种开发和测试需求

帖子 (Posts)

提供模拟的文章数据,包含标题、内容、作者ID等字段

/posts

评论 (Comments)

模拟的评论数据,与帖子相关联,包含邮箱、内容等信息

/comments

用户 (Users)

模拟的用户数据,包含姓名、邮箱、地址、公司等详细信息

/users

相册 (Albums)

模拟的相册数据,包含标题和所属用户ID

/albums

照片 (Photos)

模拟的照片数据,包含标题、URL和缩略图URL

/photos

待办事项 (Todos)

模拟的待办事项数据,包含标题和完成状态

/todos

优点

  • 完全免费,无需任何费用即可使用全部功能
  • 无需注册账号或获取API密钥,即开即用
  • 支持所有标准HTTP方法:GET、POST、PUT、PATCH、DELETE
  • 提供跨域资源共享(CORS)支持,可在任何前端项目中使用
  • 响应速度快,稳定性高,适合开发和演示环境使用
  • 支持查询参数过滤,如按用户ID筛选帖子
  • 开源项目,托管在GitHub上,透明度高

缺点

  • 数据是静态的,POST/PUT/DELETE操作不会真正修改服务器数据
  • 不适合生产环境使用,仅用于开发和测试
  • 数据量有限,每种资源类型只有固定数量的记录
  • 不支持复杂查询,如排序、分页等高级功能
  • 没有官方的SLA保证,可能会有不定期的维护或 downtime
  • 不能自定义数据结构,只能使用预设的资源类型
  • 没有用户认证机制,不适合测试需要权限验证的场景

使用方法

简单几步,即可在你的项目中使用 JSONPlaceholder

基本使用

JSONPlaceholder 无需注册,直接通过HTTP请求即可使用。基础URL为:

https://jsonplaceholder.typicode.com

你可以通过添加不同的端点来获取不同类型的数据:

获取所有帖子:

GET https://jsonplaceholder.typicode.com/posts

获取ID为1的帖子:

GET https://jsonplaceholder.typicode.com/posts/1

获取ID为1的帖子的所有评论:

GET https://jsonplaceholder.typicode.com/posts/1/comments
JavaScript 示例
// 使用 fetch API 获取帖子
fetch('https://jsonplaceholder.typicode.com/posts/1')
  .then(response => response.json())
  .then(json => console.log(json))
  .catch(error => console.error('Error:', error));
jQuery 示例
// 使用 jQuery 获取帖子
$.get('https://jsonplaceholder.typicode.com/posts/1', function(data) {
  console.log(data);
})
.fail(function() {
  console.error('Error fetching data');
});
React 示例
// React 组件中使用
import { useEffect, useState } from 'react';

function Post() {
  const [post, setPost] = useState(null);
  
  useEffect(() => {
    fetch('https://jsonplaceholder.typicode.com/posts/1')
      .then(res => res.json())
      .then(data => setPost(data));
  }, []);
  
  return post && (
    

{post.title}

{post.body}

); }
提交数据示例
// 提交新帖子(模拟)
fetch('https://jsonplaceholder.typicode.com/posts', {
  method: 'POST',
  body: JSON.stringify({
    title: '新帖子标题',
    body: '帖子内容',
    userId: 1
  }),
  headers: {
    'Content-type': 'application/json; charset=UTF-8',
  },
})
.then(response => response.json())
.then(json => console.log(json));

社区评价

来自全球开发者的真实评价

4.8/5
平均评分
10k+
每周活跃用户
98%
用户满意度

评分分布

5 星
85%
4 星
10%
3 星
3%
2 星
1%
1 星
1%

喜欢 JSONPlaceholder 吗?

收藏起来或分享给你的开发者朋友