You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过jsonplaceholder API获取指定帖子的评论列表?

获取JSONPlaceholder中帖子的关联评论方法

JSONPlaceholder提供了两种直接的方式获取指定帖子的评论列表,核心是利用你已获取到的帖子id字段:

方法1:使用嵌套资源端点

这是符合REST风格的标准方式,通过帖子ID构造嵌套路径即可:

  • 请求格式:/posts/{postId}/comments
  • 示例(假设选中帖子的id为1):
// 用Fetch API请求评论
fetch('/posts/1/comments')
  .then(response => response.json())
  .then(comments => console.log(comments))
  .catch(error => console.error(error));

这个端点会直接返回对应帖子的所有评论数据。

方法2:使用查询参数过滤

你也可以直接请求全局评论列表,通过postId参数筛选目标帖子的评论:

  • 请求格式:/comments?postId={postId}
  • 示例(假设选中帖子的id为2):
curl '/comments?postId=2'

返回结果同样是该帖子关联的全部评论。

两种方法效果完全一致,可根据你的代码结构选择更适配的方式。

内容的提问来源于stack exchange,提问作者Nupur Rawat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 12:10:29