开发者

How to Convert Json Value of Http Post Parameter to Python Dict in Django?

开发者 https://www.devze.com 2023-04-06 05:23 出处:网络
I am using Djan开发者_Go百科go 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 ch

I am using Djan开发者_Go百科go 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"
}
}"


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号