Python Json

开始

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# -*-coding:utf-8-*-
import json

from httptst.Singleton import singleton


@singleton
class JsonHelper(object):
name = 'json helper'

def convert_to_builtin_type(obj):
print 'default(', repr(obj), ')'
dict = {}
dict.update(obj.__dict__)
return dict

# obj 转 json
def getJson(self, obj):
data = json.dumps(obj, sort_keys=True, default=self.convert_to_builtin_type)
return data

# json str 转dict
def parse(self, jsonStr):
jsonDict = json.loads(jsonStr)
return jsonDict
------ 本文结束 🎉🎉 谢谢观看  ------