Browse Source

python json

tobby48 5 years ago
parent
commit
aff061c068

+ 5
- 0
src/main/python/kr/co/swh/lecture/opensource/json/json-dump-file.py View File

@@ -0,0 +1,5 @@
1
+import json
2
+
3
+x = { "name":"SWH", "age":3, "subject":"코딩" }
4
+with open('sample.json', 'w', encoding='utf-8') as f:   # Window운영체제의 인코딩으로 오픈 시 한글이 깨짐
5
+    json.dump(x, f, ensure_ascii=False)     # ensure_ascii 기본값이 True, False로 해야 문자 그대로 출력

+ 8
- 0
src/main/python/kr/co/swh/lecture/opensource/json/json-dump-pretty.py View File

@@ -0,0 +1,8 @@
1
+import json
2
+x = { "name":"SWH", "age":3, "subject":"Coding" }
3
+y = json.dumps(x, indent=4)
4
+z = json.dumps(x, indent=4, sort_keys=True)
5
+
6
+print(x)
7
+print(y)
8
+print(z)

+ 8
- 0
src/main/python/kr/co/swh/lecture/opensource/json/json-dump.py View File

@@ -0,0 +1,8 @@
1
+import json
2
+x = { "name":"SWH", "age":3, "subject":"Coding" }
3
+y = json.dumps(x)
4
+
5
+print(x)
6
+print(y)
7
+print(x["name"])    # 딕셔너리 타입
8
+#print(y["name"])    # ?

+ 5
- 0
src/main/python/kr/co/swh/lecture/opensource/json/json-load-file.py View File

@@ -0,0 +1,5 @@
1
+import json
2
+
3
+with open('json.json', encoding='utf-8') as f:   # Window운영체제의 인코딩으로 오픈 시 한글이 깨짐
4
+    x = json.load(f)
5
+    print(x)

+ 8
- 0
src/main/python/kr/co/swh/lecture/opensource/json/json-load.py View File

@@ -0,0 +1,8 @@
1
+import json
2
+x = '{ "name":"SWH", "age":3, "subject":"Coding" }'
3
+y = json.loads(x)
4
+
5
+print(x)
6
+print(y)
7
+print(y["name"])    # 딕셔너리 타입
8
+#print(x["age"])    # ?

+ 16
- 0
src/main/python/kr/co/swh/lecture/opensource/json/json.json View File

@@ -0,0 +1,16 @@
1
+{
2
+    "students": 
3
+    [
4
+	    {
5
+	        "name": "tobby48",
6
+			"age": 32,
7
+			"addresses": ["안흥동", "증포동"]
8
+	    },
9
+	    {
10
+	        "name": "hama",
11
+			"age": 30,
12
+			"addresses": ["백사면", "부발읍"]
13
+	    }
14
+    ],
15
+    "academyName":"swhacademy"
16
+}