程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python format processing --json

編輯:Python

json character string

  1. menu = \
  2. {
  3. "breakfast":{
  4. "hours":"7-11",
  5. "items":{
  6. "breakfast burritos":"$6.00",
  7. "pancakes":"$4.00"
  8. }
  9. },
  10. "lunch":{
  11. "hours":"11-3",
  12. "items":{
  13. "hamburger":"$5.00"
  14. }
  15. },
  16. "dinner":{
  17. "hours":"3-10",
  18. "items":{
  19. "spaghetti":"$8.00"
  20. }
  21. }
  22. }

  1. import json
  2. menu_json = json.dumps(menu)
  3. menu_json
  4. menu2 = json.loads(menu_json)# It can be interpreted as python structure
  5. import datetime
  6.     now = datetime.datetime.utcnow()
  7. json.dumps(now)# Unable to convert , Because of the standard json No date defined
  8. # transformation
  9. now_str = str(now)
  10. json.dumps(now_str)# You can convert
  11. from time import mktime
  12. now_epoch = int(mktime(now.timetuple()))
  13. json.dumps(now_epoch)# Can convert epoch value
  14. classDTEncoder(json.JSONEncoder):# Inherit overloads default Method
  15. def default(self, obj):
  16. # isinstance() Check obj The type of
  17. if isinstance(obj, datetime.datetime):
  18. return int(mktime(obj.timetuple()))
  19. # Otherwise, it's what ordinary decoders know :
  20. return json.JSONEncoder.default(self, obj)
  21. json.dumps(now, cls=DTEncoder)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved