urllib的urlencode返回奇怪的编码结果

|| 我正在尝试使用Facebook的REST API,并使用
urllib.urlencode
编码JSON字符串/字典。但是,我得到的结果与正确的编码结果有所不同(通过将字典粘贴到http://developers.facebook.com/docs/reference/rest/stream.publish/这里的附件字段中来显示)。我想知道是否有人可以提供任何帮助。 谢谢。 编辑: 我正在尝试对以下字典进行编码:
{\"media\": [{\"type\":\"flash\", \"swfsrc\":\"http://shopperspoll.webfactional.com/media/flashFile.swf\", \"height\": \'100\', \"width\": \'100\', \"expanded_width\":\"160\", \"expanded_height\":\"120\", \"imgsrc\":\"http://shopperspoll.webfactional.com/media/laptop1.jpg\"}]}
这是使用urllib.urlencode编码的字符串:
\"media=%5B%7B%27swfsrc%27%3A+%27http%3A%2F%2Fshopperspoll.webfactional.com%2Fmedia%2FflashFile.swf%27%2C+%27height%27%3A+%27100%27%2C+%27width%27%3A+%27100%27%2C+%27expanded_width%27%3A+%27160%27%2C+%27imgsrc%27%3A+%27http%3A%2F%2Fshopperspoll.webfactional.com%2Fmedia%2Flaptop1.jpg%27%2C+%27expanded_height%27%3A+%27120%27%2C+%27type%27%3A+%27flash%27%7D%5D
\“ 这不是让我复制从facebook rest文档链接中抛出的结果,而是复制附件字段中的上述词典时,结果是不同的。     
已邀请:
        
urllib.encode
并非用于对单个值进行urlencoding(因为同名的函数在许多语言中),而是用于编码单独值的字典。例如,如果我有字典
{\"a\": 1, \"b\": 2}
,它将产生字符串
\"a=1&b=2\"
。 首先,您要将字典编码为JSON。
data = {\"media\": [{\"type\":\"flash\", \"swfsrc\":\"http://shopperspoll.webfactional.com/media/flashFile.swf\", \"height\": \'100\', \"width\": \'100\', \"expanded_width\":\"160\", \"expanded_height\":\"120\", \"imgsrc\":\"http://shopperspoll.webfactional.com/media/laptop1.jpg\"}]}

import json
json_encoded = json.dumps(data)
然后,您可以使用
urllib.encode
创建完整的查询字符串
import urllib
urllib.encode({\"access_token\": example, \"attachment\": json_encoded})
# produces a long string in the form \"access_token=...&attachment=...\"
或使用
urllib.quote
编码附件参数
urllib.quote(json_encoded)
# produces just the part following \"&attachment=\"
    

要回复问题请先登录注册