如何将Django中HTTP POST参数的Json值转换为Python字典?
我正在使用Django来接收和处理来自foursquare实时API的推送通知。每次签到都会作为一个POST请求发送到我的服务器,这个请求里有一个叫做checkin的参数。我想获取这个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"
}
}"
3 个回答
-1
在Python中,布尔值是以大写字母开头的,分别是True和False。
请注意这一点。
编辑:
请关注以下这几行:
"primary": true
}
],
"verified": true,
这两个“true”都是小写的,需要改成大写。
0
把 checkin = json.load(request.POST)
改成 checkin = json.loads(checkin_json)
4
试试用 json.loads(checkin_json)
,而不是 json.load(request.POST)
。注意多了一个's'。