-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy39.py
More file actions
42 lines (23 loc) · 1.18 KB
/
py39.py
File metadata and controls
42 lines (23 loc) · 1.18 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
""" json کار با """
# تفاوت های اساس فرمت پایتون با جیسان
# | python | => | Json |
# int,flo,comp => Number
# True,False => true,false
# none => Null
# dict => object
# list,tuple => Array
import json
# کار با فرمت جیسان در استرینگ
# یک داده از نوع استرینگ که داخلش کد های جیسان هست و از قانون جیسان پیروی میکند
js_format_str = '{"name": "mohammad" , "age":18}'
# تبدیل جیسان به فرمت پایتون(اسخراج داده از استرینگ)
json.loads(js_format_str)
# تبدیل پایتون(دیکشنری) به فرمت جیسان در قالب استرینگ
json.dumps({'name': None}) # => name: Null
# کار با فرمت جیسان در فایل جیسان
# تبدیل جیسان به فرمت پایتون (استخراج داده از فایل جیسان)
with open('user.json', 'r') as user:
json.load(user)
# تبدیل پایتون(دیکشنری) به فرمت جیسان برای فایل جیسان
with open('person.json', 'a') as person:
json.dump({'name': True}, person)