简体   繁体   中英

ValueError('No JSON object could be decoded',) is not JSON serializable

My code is fetching facebook information I will get this error

Traceback (most recent call last):
  File "crawler.py", line 34, in <module>
    print graph.get_information(url[i]).encode('utf-8')
  File "/weblog/workingspace/thun/facebook-api/facebook_api.py", line 23, in get_information
    return json.dumps(ret)
  File "/usr/lib64/python2.6/json/__init__.py", line 230, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib64/python2.6/json/encoder.py", line 367, in encode
    chunks = list(self.iterencode(o))
  File "/usr/lib64/python2.6/json/encoder.py", line 306, in _iterencode
    for chunk in self._iterencode_list(o, markers):
  File "/usr/lib64/python2.6/json/encoder.py", line 204, in _iterencode_list
    for chunk in self._iterencode(value, markers):
  File "/usr/lib64/python2.6/json/encoder.py", line 309, in _iterencode
    for chunk in self._iterencode_dict(o, markers):
  File "/usr/lib64/python2.6/json/encoder.py", line 275, in _iterencode_dict
    for chunk in self._iterencode(value, markers):
  File "/usr/lib64/python2.6/json/encoder.py", line 317, in _iterencode
    for chunk in self._iterencode_default(o, markers):
  File "/usr/lib64/python2.6/json/encoder.py", line 323, in _iterencode_default
    newobj = self.default(o)
  File "/usr/lib64/python2.6/json/encoder.py", line 344, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: ValueError('No JSON object could be decoded',) is not JSON serializable

My code is:

print graph.get_information("name of user").encode('utf-8')

I don't know what wrong because when i use https://developers.facebook.com/tools/explorer to get information. It's work But when I use facebook-api i got some error. Please help me. Thank you for your attention.

Ps. Sorry about my bad English

The error says "Some Type is not JSON serializable" that means the data you are getting from facebook contains a datatype which is not supported by JSON. Following datatypes are supported by JSON

  • Primitive Types
    • String
    • Number
    • Boolean
    • Null
  • Structure Type
    • Object
    • Array

The code which is actually converting the data into JSON (in your case facebook_api.py) needs to handle this problem. You can write a custom json encoder that will handle all datatypes which are not supported by JSON. for eg

class CustomJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, decimal.Decimal):
            return float(obj)
        if isinstance(obj, date):
            return obj.isoformat()
        else:
            return super(CustomJSONEncoder, self).default(obj)

In the above code I am handling 'decimal' and 'date' datatypes which are not supported by JSON. Then you need to use this custome JSON encoder in your call to 'json.dumps'

json.dumps(input_dict, cls=CustomJSONEncoder)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM