Pythonjson error xxisnotJSONserializable solution

Source: Internet
Author: User
This article describes how to solve the Pythonjson error xxisnotJSONserializable. For more information about the Python json error xx is not JSON serializable, see the following article, for more information, see

Python json error xx is not JSON serializable solution

When using json, xxx is not JSON serializable is often encountered, that is, some objects cannot be serialized. Django users often know that django has a built-in Encoder to serialize time and other common objects. In fact, we can define the serialization of specific types of objects by ourselves. let's take a look at how to define and use them.


#! /Usr/bin/env python #-*-coding: UTF-8-*-# json_extention #2014-03-16 # copyright: orangleliu # license: the dumps method in BSD ''' python is very useful. we can directly serialize our dict to a json object, but sometimes we cannot serialize it by adding some custom classes, in this case, you need to customize some serialization methods. for example, In [3]: from datetime import datetime In [4]: json_1 = {'num': 1112, 'Date ': datetime. now ()} In [5]: import json In [6]: json. dumps (json_1) parse TypeError Traceback (most recent call last) D: \ devsofts \ python2.7 \ lib \ site-packages \ django \ core \ management \ commands \ shell. py c in
 
  
() ----> 1 json. dumps (json_1) TypeError: datetime. datetime (2014, 3, 16, 13, 47, 37,353 000) is not JSON serial izable ''' from datetime import json class DateEncoder (json. JSONEncoder): def default (self, obj): if isinstance (obj, datetime): return obj. str () return json. JSONEncoder. default (self, obj) json_1 = {'Num ': 1112, 'Date': datetime. now ()} print json. dumps (json_1, cls = DateEncoder) ''' output: ps d: \ code \ python \ python_abc> python. \ json_extention.py {"date": "13:56:39. 003000 "," num ": 1112} ''' # Let's try to customize a class User (object): def init (self, name): self. name = name class UserEncoder (json. JSONEncoder): def default (self, obj): if isinstance (obj, User): return obj. name return json. JSONEncoder. default (self, obj) json_2 = {'user': user ('orange')} print json. dumps (json_2, cls = UserEncoder) ''' ps d: \ code \ python \ python_abc> python. \ json_extention.py {"date": "14:01:46. 738000 "," num ": 1112} {" user ":" orangle "}'''
 

The definition processing method inherits a subclass of json. JSONEncoder and adds a custom processing method to the cls function of the dumps method.

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

The above is the details about the Python json error xx is not JSON serializable solution. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.