Python JSON error XX is not json serializable workaround introduction

Source: Internet
Author: User
This article mainly introduces the Python JSON error XX is not JSON serializable solution of the relevant information, the need for friends can refer to the following

Python JSON error XX is not json serializable workaround

When using JSON, you often encounter xxx is not JSON serializable, which means that some objects cannot be serialized. Often, Django's classmates know that Django has a self-contained encoder to serialize time and other commonly used objects. In fact, we can define the serialization of certain types of objects by ourselves, and see how we define and use them.


#!/usr/bin/env python #-*-coding:utf-8-*-#json_extention #2014 -03-16 #copyright: Orangleliu #license: BSD "" Pyth On dumps method is very useful, can directly serialize our dict directly into JSON object but sometimes we add some custom classes can not be serialized, this time need to customize some of the serialization method reference: Http://www.php.cn/For example: in [3]: From datetime import datetime in [4]: json_1 = {' num ': 1112, ' Date ':d Atetime.now ()} in [5]: Import JSON in [6]: Json.dum  PS (json_1)---------------------------------------------------------------------------TypeError Traceback (most recent) D:\devsofts\python2.7\lib\site-packages\django\core\management\commands\shell.py C in <module> ()----> 1 Json.dumps (json_1) TypeError:datetime.datetime (3, +, +, A, Notoginseng, 353000) is not JSON serial izable "' From date Time Import 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 ':d atetime.now ()} printJson.dumps (json_1, Cls=dateencoder) "Output: PS d:\code\python\python_abc> python. \json_extention.py {" Date ":" 2   014-03-16 13:56:39.003000 "," num ": 1112}" #我们自定义一个类试试 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 (' Orangle ')} print Json.dumps (json_2, Cls=userencoder) ' PS d:\c Ode\python\python_abc> python. \json_extention.py {"date": "2014-03-16 14:01:46.738000", "num": 1112} {"User": " Orangle "}"

Defining the processing method is to inherit the JSON. A subclass of Jsonencoder that is used when a custom processing method is added to the CLS function of the dumps method.

Thank you for reading, hope to help everyone, thank you for the support of this site!

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.