如何在Django中将Http Post参数的Json值转换为Python Dict?

发布于 2024-12-05 17:06:55 字数 1801 浏览 2 评论 0原文

我正在使用 Django 接收和处理来自 foursquare 实时 api 的推送通知。每个签入都会作为 POST 请求推送到我的服务器,其中包含名为签入的单个参数。我试图获取 checkin 参数的值并将其转换为 python 字典。但是,调用 json.loads 总是会导致以下错误:

NameError: name 'true' is not defined

我知道 json 是有效的,所以我一定做错了什么。

代码是:

import json    
def push(request):
  if request.is_secure():
    checkin_json = request.POST['checkin']
    checkin = json.load(request.POST)

post请求的正文是:

"checkin = 
{
"id": "4e6fe1404b90c00032eeac34",
"createdAt": 1315955008,
"type": "checkin",
"timeZone": "America/New_York",
"user": {
    "id": "1",
    "firstName": "Jimmy",
    "lastName": "Foursquare",
    "photo": "https://foursquare.com/img/blank_boy.png",
    "gender": "male",
    "homeCity": "New York, NY",
    "relationship": "self"
},
"venue": {
    "id": "4ab7e57cf964a5205f7b20e3",
    "name": "foursquare HQ",
    "contact": {
        "twitter": "foursquare"
    },
    "location": {
        "address": "East Village",
        "lat": 40.72809214560253,
        "lng": -73.99112284183502,
        "city": "New York",
        "state": "NY",
        "postalCode": "10003",
        "country": "USA"
    },
    "categories": [
        {
            "id": "4bf58dd8d48988d125941735",
            "name": "Tech Startup",
            "pluralName": "Tech Startups",
            "shortName": "Tech Startup",
            "icon": "https://foursquare.com/img/categories/building/default.png",
            "parents": [
                "Professional & Other Places",
                "Offices"
            ],
            "primary": true
        }
    ],
    "verified": true,
    "stats": {
        "checkinsCount": 7313,
        "usersCount": 565,
        "tipCount": 128
    },
    "url": "http://foursquare.com"
}
}"

I am using Django to receive and process push notifications from the foursquare real-time api. Each checkin is pushed as a POST request to my server containing a single parameter named checkin. I am trying to grab the value of the checkin parameter and convert it to a python dict. However, calling json.loads always results in the following error:

NameError: name 'true' is not defined

I know the json is valid, so I must be doing something wrong.

The code is:

import json    
def push(request):
  if request.is_secure():
    checkin_json = request.POST['checkin']
    checkin = json.load(request.POST)

The body of the post request is:

"checkin = 
{
"id": "4e6fe1404b90c00032eeac34",
"createdAt": 1315955008,
"type": "checkin",
"timeZone": "America/New_York",
"user": {
    "id": "1",
    "firstName": "Jimmy",
    "lastName": "Foursquare",
    "photo": "https://foursquare.com/img/blank_boy.png",
    "gender": "male",
    "homeCity": "New York, NY",
    "relationship": "self"
},
"venue": {
    "id": "4ab7e57cf964a5205f7b20e3",
    "name": "foursquare HQ",
    "contact": {
        "twitter": "foursquare"
    },
    "location": {
        "address": "East Village",
        "lat": 40.72809214560253,
        "lng": -73.99112284183502,
        "city": "New York",
        "state": "NY",
        "postalCode": "10003",
        "country": "USA"
    },
    "categories": [
        {
            "id": "4bf58dd8d48988d125941735",
            "name": "Tech Startup",
            "pluralName": "Tech Startups",
            "shortName": "Tech Startup",
            "icon": "https://foursquare.com/img/categories/building/default.png",
            "parents": [
                "Professional & Other Places",
                "Offices"
            ],
            "primary": true
        }
    ],
    "verified": true,
    "stats": {
        "checkinsCount": 7313,
        "usersCount": 565,
        "tipCount": 128
    },
    "url": "http://foursquare.com"
}
}"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玉环 2024-12-12 17:06:55

尝试使用 json.loads(checkin_json) 而不是 json.load(request.POST)。注意额外的“s”。

Try json.loads(checkin_json) instead of json.load(request.POST). Notice the extra 's'.

遮云壑 2024-12-12 17:06:55

checkin = json.load(request.POST) 更改为 checkin = json.loads(checkin_json)

change checkin = json.load(request.POST) to checkin = json.loads(checkin_json)

痴情换悲伤 2024-12-12 17:06:55

在 python 上,布尔值是大写的(第一个字母是大写):True/False。

检查一下。

编辑:
注意这一行:

"primary": true
    }
],
"verified": true,

两个“true”值都是小写的,需要大写

On python, boolean values are Capitalized (first letter is uppercase): True/False.

Check this.

EDIT:
Pay attentiot at this lines:

"primary": true
    }
],
"verified": true,

Both "true" values are lowercase and need to be capitalized

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文