-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython json.py
More file actions
52 lines (46 loc) · 1.28 KB
/
python json.py
File metadata and controls
52 lines (46 loc) · 1.28 KB
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
'''JSON is a syntax for storing and exchanging data.
JSON is text, written with JavaScript object notation'''
import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y["age"])
print()
# a Python object (dict):
x = {"name": "John","age": 30, "city": "New York"}
# convert into JSON:
y = json.dumps(x)
# the result is a JSON string:
print(y)
print()
print(json.dumps({"name": "John", "age": 30}))
print(json.dumps(["apple", "bananas"]))
print(json.dumps(("apple", "bananas")))
print(json.dumps("hello"))
print(json.dumps(42))
print(json.dumps(31.76))
print(json.dumps(True))
print(json.dumps(False))
print(json.dumps(None))
'''When you convert from Python to JSON, Python objects are converted
into the JSON (JavaScript) equivalent:
Python JSON
dict Object
list Array
tuple Array
str String
int Number
float Number
True true
False false
None null'''
x= {"name": "John","age": 30, "married": True, "divorced": False, "children": ("Ann","Billy"), "pets": None,
"cars": [{"model": "BMW 230", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 24.1} ]}
print(json.dumps(x))
print(json.dumps(x, indent=3))
print()
print(json.dumps(x, indent=4, separators=(". ", " = ")))
print()
print(json.dumps(x, indent=4, sort_keys=True))