0

我从这样的 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'

请帮我看看我在这里做错了什么

4

1 回答 1

1

在您的代码x == "data"中,然后v是字典出现在json字符串中的“数据”前面,所以您应该使用v["meetingID"]而不是x.meetingID等等...

于 2013-03-26T06:46:58.020 回答