活到老,学到老
分类: Python/Ruby
2016-08-07 20:50:38
| JSON | Python |
|---|---|
| object | dict |
| array | list |
| string | str |
| number (int) | int |
| number (real) | float |
| true | True |
| false | False |
| null | None |
#!/usr/local/bin/python3
#coding=utf-8
import json
def encodeJson():
test1_contents_dict = {"a": 1,"b": 2 ,"c": 3}
test2_contents_list=[4,5,6]
test_contents_dict={"test1":test1_contents_dict,"test2":test2_contents_list}
test_dict={"test":test_contents_dict}
test_json_data=json.dumps(test_dict,sort_keys=True)
print(test_json_data)
with open('E:\\test.json', 'w') as f:
json.dump(test_dict,f,indent='\t',sort_keys=True)
def decodeJson():
with open('E:\\test.json','r') as f:
test_dict=json.load(f)
test_contents_dict=test_dict['test'] print(test_contents_dict)
test1_contents_dict = test_contents_dict['test1'] print(test1_contents_dict)
test2_contents_list=test_contents_dict['test2'] print(test2_contents_list) #print(test_dict['test']['test1']) #print(test_dict['test']['test2'])
if __name__ == '__main__': encodeJson() decodeJson()