-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_json.py
executable file
·59 lines (48 loc) · 1 KB
/
file_json.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# Module: file_json.py
# Purpose: File json tests
# Date: N/A
# Notes:
# 1) tbd
# Ref:
# https://docs.python.org/2/library/json.html
#
import json
import os
MYFILE="fj_test.json"
print "Create some strings:"
str1=[1, 2, 'Inner 1']
str2=['Inner 2']
str3=[3, str1, str2, 'Outer']
print " 1: ", str1
print " 2: ", str2
print " 3: ", str3
print "Encode str3 as JSON"
js1 = json.dumps(str3)
print " js1: ", js1
str4=['foo', {'bar': ('baz', None, 1.0, 2)}]
js2 = json.dumps(str4)
print " js2: ", js2
print "Access some elements of str4:"
a = str4[0]
b = str4[1]
c = str4[1]['bar']
d = str4[1]['bar'][0]
print " ", a, b, c, d
print "Persist to a file:"
fo = open(MYFILE, "w")
print "FO=", fo
json.dump(str4, fo)
fo.close()
print "Read back from the file:"
fi = open(MYFILE, "r")
print "FI=", fi
str5=json.load(fi)
fi.close()
print " str5: ", str5
print "Access some elements of str4:"
a = str5[0]
b = str5[1]
c = str5[1]['bar']
d = str5[1]['bar'][0]
print " ", a, b, c, d