Why I can't use urlencode to encode json format data?

冷暖自知 提交于 2020-01-12 03:29:30

问题


I have a problem about urlencode in python 2.7:

>>> import urllib
>>> import json
>>> urllib.urlencode(json.dumps({'title':"hello world!",'anonymous':False,'needautocategory':True}))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/urllib.py", line 1280, in urlencode
    raise TypeError
TypeError: not a valid non-string sequence or mapping object

回答1:


Because urllib.urlencode "converts a mapping object or a sequence of two-element tuples to a “percent-encoded” string...". Your string is neither of these.

I think you need urllib.quote or urllib.quote_plus.




回答2:


urlencode can encode a dict, but not a string. The output of json.dumps is a string.

Depending on what output you want, either don't encode the dict in JSON:

>>> urllib.urlencode({'title':"hello world!",'anonymous':False,'needautocategory':True})
'needautocategory=True&anonymous=False&title=hello+world%EF%BC%81'

or wrap the whole thing in a dict:

>>> urllib.urlencode({'data': json.dumps({'title':"hello world!",'anonymous':False,'needautocategory':True})})
'data=%7B%22needautocategory%22%3A+true%2C+%22anonymous%22%3A+false%2C+%22title%22%3A+%22hello+world%5Cuff01%22%7D'

or use quote_plus() instead (urlencode uses quote_plus for the keys and values):

>>> urllib.quote_plus(json.dumps({'title':"hello world!",'anonymous':False,'needautocategory':True}))
'%7B%22needautocategory%22%3A+true%2C+%22anonymous%22%3A+false%2C+%22title%22%3A+%22hello+world%5Cuff01%22%7D'



回答3:


json.dumps() returns a string.

urllib.urlencode() expects a query in the format of a mapping object or tuples. Note that it does not expect a string.

You're passing the first as the parameter for the second, resulting in the error.




回答4:


import libraries

import request
import json

spec is a dictionary object

spec = {...}

convert dictionary object to json

data = json.dumps(spec, ensure_ascii=False)

and finally do request with parameter spec in json format

response = requests.get(
    'http://localhost:8080/...',
    params={'spec': data}
)

analyze response ...




回答5:


For those of ya'll getting the error:

AttributeError: module 'urllib' has no attribute 'urlencode'

It's because urllib has been split up in Python 3


Here's the code for Python 3:

import urllib.parse
dict = {
    "title": "Hello world",
    "anonymous": False,
    "needautocategory": True
}
urllib.parse.urlencode(dict)  # 'title=Hello+world&anonymous=False&needautocategory=True'


来源:https://stackoverflow.com/questions/8293113/why-i-cant-use-urlencode-to-encode-json-format-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!