首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在python中编写正确的json

如何在python中编写正确的json
EN

Stack Overflow用户
提问于 2020-07-10 21:25:27
回答 2查看 47关注 0票数 0

谁能给我解释一下为什么我得到了422错误?

我有来自Postman的JSON,现在我正试着在Locust中发送这个JSON。BUt所有的时间,我得到422错误。

这是来自Postman的JSON:

代码语言:javascript
运行
复制
data = {"startDate": "2020-10-01T00:00:00.000Z",
                "endDate": "2020-10-11T00:00:00.000Z",
                "id": 117,
                "title": "Job Postmanowy Contractora 156",
                "desc": "Backendowe opowiastki",
                "active": true,
                "isDraft": false,
                "isPaused": false,
                "isBlocked": false,
                "locationPayload": "{\"locationType\":\"locationPlace\",\"locationName\":\"Wałbrzych, Dolnośląskie, Poland\",\"locationPlace\":\"place.8245570224125220\",\"locationRegion\":\"region.11228101935550230\",\"locationCountry\":\"country.5811537771766020\"}",
                "locationName": "Minneapolis, MN, USA",
                "workingHours": "No matter",
                "employmentType": "other",
                "assignmentType": "indi",
                "gender": "both",
                "minWorkers": 3,
                "maxWorkers": 5,
                "minHeight": 10,
                "minWeight": 10,
                "maxHeight": 180,
                "maxWeight": 80,
                "minAge": 18,
                "maxAge": 25,
                "minWage": 20,
                "maxWage": 20,
                "wageFrequency": "per hour",
                "paymentType": "credit card",
                "updatedBy": null,
                "createdAt": "2020-07-10T12:34:18.000Z",
                "updatedAt": "2020-07-10T12:34:20.000Z",
                "deletedAt": null,
                "userId": 100,
                "canApply": false,
                "applied": false,
                "applicationStatus": null,
                "isMine": true,
                "status": "future",
                "isFavourite": false,
                "applications": {"applied": 0, "accepted": 0, "declined": 0},
                "isEditable": true,
                "location": {},
                "user": {"id": 100,
                         "fullName": "Con SzamsungS9 1",
                         "companyName": "", "rating": null,
                         "avatar": "files/3859ca8b6afde6838cf3b6fce356dbaf67359d252f5670d87d6a61e3c7149377image-e4212eac-e0b8-40e7-b009-d3907bf51a5a.jpg"},
                "languages": ["German", "English", "Spanish", "Hindi", "Italian", "Polish"],
                "highlights": ["high-voltage", "heavy materials"]}

使用小写boolean时,我有创建参数的提示,当我更改为大写时,我有422个参数。

这就是我想发布我的JSON的方式

代码语言:javascript
运行
复制
self.client.post('/v1/jobs/drafts/', headers=self.headers, data=json.dumps(data))
EN

回答 2

Stack Overflow用户

发布于 2020-07-10 21:40:38

这应该是可行的:

首先,使用python语法将json创建为字典:

代码语言:javascript
运行
复制
data = {"startDate": "2020-10-01T00:00:00.000Z",
                "endDate": "2020-10-11T00:00:00.000Z",
                "id": 117,
                "title": "Job Postmanowy Contractora 156",
                "desc": "Backendowe opowiastki",
                "active": True,
                "isDraft": False,
                "isPaused": False,
                "isBlocked": False,
                "locationPayload": "{\"locationType\":\"locationPlace\",\"locationName\":\"Wałbrzych, Dolnośląskie, Poland\",\"locationPlace\":\"place.8245570224125220\",\"locationRegion\":\"region.11228101935550230\",\"locationCountry\":\"country.5811537771766020\"}",
                "locationName": "Minneapolis, MN, USA",
                "workingHours": "No matter",
                "employmentType": "other",
                "assignmentType": "indi",
                "gender": "both",
                "minWorkers": 3,
                "maxWorkers": 5,
                "minHeight": 10,
                "minWeight": 10,
                "maxHeight": 180,
                "maxWeight": 80,
                "minAge": 18,
                "maxAge": 25,
                "minWage": 20,
                "maxWage": 20,
                "wageFrequency": "per hour",
                "paymentType": "credit card",
                "updatedBy": None,
                "createdAt": "2020-07-10T12:34:18.000Z",
                "updatedAt": "2020-07-10T12:34:20.000Z",
                "deletedAt": None,
                "userId": 100,
                "canApply": False,
                "applied": False,
                "applicationStatus": None,
                "isMine": True,
                "status": "future",
                "isFavourite": False,
                "applications": {"applied": 0, "accepted": 0, "declined": 0},
                "isEditable": True,
                "location": {},
                "user": {"id": 100,
                         "fullName": "Con SzamsungS9 1",
                         "companyName": "", "rating": None,
                         "avatar": "files/3859ca8b6afde6838cf3b6fce356dbaf67359d252f5670d87d6a61e3c7149377image-e4212eac-e0b8-40e7-b009-d3907bf51a5a.jpg"},
                "languages": ["German", "English", "Spanish", "Hindi", "Italian", "Polish"],
                "highlights": ["high-voltage", "heavy materials"]}

然后使用关键字json而不是data

代码语言:javascript
运行
复制
self.client.post('/v1/jobs/drafts/', headers=self.headers, json=data)

来源:

https://docs.locust.io/en/stable/api.html#locust.clients.HttpSession.post

票数 0
EN

Stack Overflow用户

发布于 2020-07-14 03:04:01

将JSON存储在.json文件中,现在您可以直接从该文件读取并运行测试。适用于我:)

代码语言:javascript
运行
复制
import json

    @task(1)
    def my_task(self):
         file_name = 'my_json.json'
         with open(file_name) as json_file:
            post_data = json.load(json_file)
            self.client.post('/v1/jobs/drafts/', data=json.dumps(post_data),
                          headers=my_headers)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62835294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档