2011-09-20 62 views
2

我正在使用Django從foursquare實時api接收和處理推送通知。每個簽入都會作爲POST請求推送到包含名爲checkin的單個參數的服務器。我試圖抓住checkin參數的值並將其轉換爲python字典。但是,調用json.loads總是導致以下錯誤:如何在Django中將Http Post參數的Json值轉換爲Python Dict?

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" 
} 
}" 

回答

4

嘗試json.loads(checkin_json)而不是json.load(request.POST)。注意額外的''。

0

變化checkin = json.load(request.POST)checkin = json.loads(checkin_json)

-1

關於Python,布爾值資本(第一個字母是大寫): 真假。

選中此項。

編輯: 在此行收費attentiot:

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

兩個 「真」 值是小寫的,需要資本

相關問題