Copyright Statement: This is an original copy of Colin Cai. Please post it.To transfer, you must indicate the original web address http://www.cnblogs.com/Colin-Cai/p/12741423.html Author: Window QQ/WeChat: 6679072 E-mail: 6679072@qq.com
Sometimes we write a Python program, when dealing with complex objects, sometimes debugging in the process, we need to see what the resulting object is. Sometimes we can print it as a json. That's a good idea.
Writing a separate program to print json for each object is a non-reusable job, and we can consider common writing, but Python's reflection can help us do this. Here's what I wrote overnight. All member names are printed in dictionary format.
from functools import reduce make_json = lambda blanks,obj : \ (lambda t, cut : \ 'null' if obj==None \ else str(obj) if t in (int,float) \ else ('true' if obj else 'false') if t==bool \ else '"%s"'%obj if t==str \ else '[' + cut(reduce(lambda r,x:r+',\n'+' '*(blanks+2)+make_json(blanks+2,x), obj, '')) \ + '\n' + ' '*blanks + ']' if t in (list, tuple) \ else '{' + cut(reduce(lambda r,x:r+',\n'+' '*(blanks+2)+'"%s" : '%x+make_json(blanks+2,obj[x]), \ sorted(filter(lambda x:type(x)==str,obj.keys())), '')) + '\n' + ' '*blanks + '}' if t==dict \ else reduce(lambda r,x:r+'%02x'%x, list(map(int, obj)),'"')+'"' if t==bytes \ else '{' + cut(reduce(lambda r,x:\ r+',\n'+' '*(blanks+2)+'"%s" : '%x+make_json(blanks+2,obj.__dict__[x]), \ sorted(filter(lambda x:len(x)<4 or x[:2]!='__' \ or x[-2:]!='__',obj.__dict__.keys())), '')) + '\n' + ' '*blanks + '}') \ (type(obj), lambda x:x if x=='' else x[1:]) print_json = lambda obj, fprint : fprint(make_json(0, obj))
The print_json above is a function of printing json. fprint is a function of printing strings. Of course, you can print anywhere you like.
We tested the following:
class class1: def __init__(self): self.a1 = None self.b1 = None self.c1 = None class class2: def __init__(self): self.a2 = None self.b2 = None self.c2 = None class class3: def __init__(self): self.a3 = None self.b3 = None self.c3 = None test_obj = class1() test_obj.a1 = [1, None, 2.5, class2(), True] test_obj.a1[3].a2 = [class3(), class3()] test_obj.a1[3].a2[0].a3 = [1, 2, 3] test_obj.a1[3].a2[0].b3 = ["test1", "test2"] test_obj.a1[3].a2[0].c3 = None test_obj.a1[3].a2[1].a3 = [5, 6, 7] test_obj.a1[3].a2[1].b3 = ["test3", "test4"] test_obj.a1[3].a2[1].c3 = [True, False] test_obj.a1[3].b2 = {"x":1, "y":2} test_obj.a1[3].c2 = type('', (), {"x":10, "y":20}) test_obj.b1 = 100 test_obj.c1 = "test" print_json(test_obj, print)
The above builds a test_obj object and prints it out.
{ "a1": [ 1, null, 2.5, { "a2": [ { "a3": [ 1, 2, 3 ], "b3": [ "test1", "test2" ], "c3": null }, { "a3": [ 5, 6, 7 ], "b3": [ "test3", "test4" ], "c3": [ true, false ] } ], "b2": { "x": 1, "y": 2 }, "c2": { "x": 10, "y": 20 } }, true ], "b1": 100, "c1": "test" }
This is a standard json, obviously the print_json function is available.Prit is a command in Python2, so it needs to be wrapped up.
If the above wants to print to a file, you can
json_file = open('test_obj.json', 'w') print_json(test_obj, lambda s:print(s, file=json_file)) json_file.close()
Disclaimer: The above code author is my own (Colin Cai), can be used freely and without any restrictions, and has nothing to do with the author if any errors or losses occur!