无法解码任何JSON对象-tastype-cu

2024-04-29 12:49:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我一字不差地遵循tastypie教程,直到我到达了文章的部分: http://django-tastypie.readthedocs.org/en/latest/interacting.html#creating-a-new-resource-post

当我运行这个命令时,我一直得到以下错误: No JSON object could be decoded

我检查过了,我确信我是逐字逐句地遵循文档的。

谢谢你的帮助


Tags: djangoorgcreatinghttpnewhtml文章readthedocs
1条回答
网友
1楼 · 发布于 2024-04-29 12:49:00

原来是个有卷曲的窗户。

  1. JSON数据应该用双引号(“”)引用,而不是单引号。
  2. json包中的所有双引号都必须用反斜杠(\)转义

例:那么,这个:

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"body": "This will prbbly be my lst post.", "pub_date": "2011-05-22T00:46:38", "slug": "another-post", "title": "Another Post", "user": "/api/v1/user/1/"}' http://localhost:8000/api/v1/entry/

应该是:

curl --dump-header - -H "Content-Type: application/json" -X POST --data "{\"body\": \"This will prbbly be my lst post.\", \"pub_date\": \"2011-05-22T00:46:38\", \"slug\": \"another-post\", \"title\": \"Another Post\", \"user\": \"/api/v1/user/1/\"}" http://localhost:8000/api/v1/entry/

相关问题 更多 >