-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_zad1.py
54 lines (47 loc) · 1.17 KB
/
json_zad1.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
# typ json - dane typu para klucz-wartość
# json => {"name" : "Radek"}
import json
print_dict = {'name': 'Radek', 'age': 40, 'czy_pali': None}
print(type(print_dict)) # <class 'dict'>
with open('nasze_dane.json', 'w') as f:
json.dump(print_dict, f)
# {"name": "Radek", "age": 40, "czy_pali": null}
# upiekszanie
with open('nasze_dane.json', 'w') as f:
json.dump(print_dict, f, indent=4)
# {
# "name": "Radek",
# "age": 40,
# "czy_pali": null
# }
# sortowanie po kluczach
with open('nasze_dane.json', 'w') as f:
json.dump(print_dict, f, indent=4, sort_keys=True)
# {
# "age": 40,
# "czy_pali": null,
# "name": "Radek"
# }
# odczyt jsona do słownika
with open('nasze_dane.json', "r") as fh:
data = json.load(fh)
print(data)
print(type(data))
# {'age': 40, 'czy_pali': None, 'name': 'Radek'}
# <class 'dict'>
print(data.get('name'))
print(data['name'])
# Radek
# Radek
json_text = json.dumps(data, indent=4, sort_keys=True)
print(json_text)
# {
# "age": 40,
# "czy_pali": null,
# "name": "Radek"
# }
dict_json = json.loads(json_text)
print(dict_json)
print(type(dict_json))
# {'age': 40, 'czy_pali': None, 'name': 'Radek'}
# <class 'dict'>