无法将JSON转换为Python对象

时间:2013-03-26 06:44:19

标签: python django json

我从这样的API获得JSON响应:

{"code": 200, 
"data": {"messageKey": {}, "returncode": "SUCCESS",
    "meetingID": "REG_561538474", "hasBeenForciblyEnded": "false", 
    "moderatorPW": "58963", "attendeePW": "54321", "message": {},
    "createTime": "1364280051568"}, "errors": null}

现在我想将其转换为Python对象并在数据库中保存一些值。

这是我的代码:

getmobject  = Meeting()
getj = json.loads(r.text)
for x,v in getj.items():
   if x == 'data':
      if x.meetingID:# == 'meetingID':
         getmobject.meetingID = x.meetingID
         if x.moderatorPW:
            getmobject.moderator_passwd = x.moderatorPW
            if x.attendeePW:
               getmobject.attendee_passwd = x.attendeePW
               getuser_main = User.objects.get(username = request.user.username)
               getmobject.name = get_meeting_name
               getmobject.created_by = getuser_main
               getmobject.sms_no = '12345'

               """Reminder we are making false as if participant accept then only
                  meeting will able to initiate"""
               getmobject.reminder = False   
               getmobject.save()

但问题是我收到了错误:

'unicode' object has no attribute 'meetingID'

请帮助我解决我在这里做错的事情

1 个答案:

答案 0 :(得分:1)

在代码x == "data"时,v是json字符串中“数据”前面出现的字典,因此您应该使用v["meetingID"]代替x.meetingID等等...