-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.json.py
65 lines (48 loc) · 1.19 KB
/
20.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
56
57
58
59
60
61
62
63
"""
JSON
JSON is a syntax for storing and exchanging data.
JSON is text, written with JavaScript object notation.
"""
# Python has a built-in package called json, which can be use to work with JSON data.
import json
# Parse JSON - Convert from JSON to Python
# If you have a JSON string, you can parse it by using the json.loads() method.
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York" }'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y)
# Convert from Python to JSON
# If you have a Python object, you can convert it into a JSON string by using
# the json.dumps() method.
# a Python object (dict):
x = {
"name": "Uma",
"age": 36,
"city": "Bangalore"
}
# convert into JSON:
y = json.dumps(x)
# the result is a JSON string:
print(y)
# You can convert Python objects of the following types, into JSON strings:
#
# dict
# list
# tuple
# string
# int
# float
# True
# False
# None
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))