1 ๋ถ„ ์†Œ์š”

JSON์ด๋ž€?

JSON์€ JavaScript Object Notation์˜ ์•ฝ์ž๋กœ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด ๋ฌธ๋ฒ•์œผ๋กœ ๊ตฌ์กฐํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๋ฌธ์ž ๊ธฐ๋ฐ˜์˜ ํ‘œ์ค€ ํฌ๋งท์ด๋‹ค.
์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ๋•Œ, ์ผ๋ฐ˜์ ์œผ๋กœ jsonํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
๊ธฐ์กด์— ์กด์žฌํ–ˆ๋˜, xml๊ณผ csvํŒŒ์ผ๊ณผ๋Š” ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ ๋ฌธ์„œ ํ˜•ํƒœ์ด๋‹ค.

JSON๋ฌธ์„œ์˜ ํ˜•ํƒœ

json์€ "ํ‚ค":"๊ฐ’"์˜ ํ˜•ํƒœ๋กœ ์ €์žฅ๋œ๋‹ค.
์ค‘๊ด„ํ˜ธ({})๋กœ ๊ฐ์‹ธ์„œ ํ‘œํ˜„ํ•˜๋ฉฐ, ์ฝค๋งˆ(,)๋ฅผ ์‚ฌ์šฉํ•ด ์—ฌ๋Ÿฌ โ€œํ‚ค:๊ฐ’โ€์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์˜ˆ์‹œ
    # Key-Value๋กœ ์ด๋ฃจ์–ด์ง„ jsonObject
    {
    "id": 1,
    "name": "helloalpaca"
    }
    

JSON์™ธ์˜ ๋‹ค๋ฅธ ๋ฌธ์„œ ํ˜•์‹

XML

XML์€ Extensible Markup Language์˜ ์•ฝ์ž๋กœ, ํ•˜๋‚˜์˜ ์–ธ์–ด ํŒŒ์ผ์ด๋‹ค.
xml์€ ๋˜ ๋‹ค๋ฅธ ๋งˆํฌ์—… ์–ธ์–ด์ธ html๊ณผ ์œ ์‚ฌํ•œ ๋ฌธ๋ฒ•์„ ๋”ฐ๋ฅด๊ณ , ํ™•์žฅ์ž๋Š” .xml์ด๋‹ค.
์˜ˆ์‹œ๋กœ๋Š” ์›น ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค ๋•Œ, pom.xmlํŒŒ์ผ์ด ์žˆ๋‹ค. xml์€ ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ html๊ณผ ๋น„์Šทํ•œ ๋ฌธ๋ฒ•์œผ๋กœ ์ €์žฅํ•˜๋‹ค๋ณด๋‹ˆ, <><> ํ˜น์€ <></>๋กœ ๋‘˜๋Ÿฌ์Œ“์ธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์•„ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE AXLContext SYSTEM "standardX10.dtd">
<AXLContext>
   <ObjectID/>
   <name>context_x10</name>
   <description>context_x10 in My Organization</description>
<objectContainerPath>/wt.inf.container.OrgContainer=My Organization</objectContainerPath>
</AXLContext>

CSV

CSV๋Š” ๋ช‡ ๊ฐ€์ง€ ํ•„๋“œ๋ฅผ ์ฝค๋งˆ(,)๋กœ ๊ตฌ๋ถ„ํ•œ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋ฐ’ ํ…์ŠคํŠธ ํŒŒ์ผ์ด๋‹ค.
ํ™•์žฅ์ž๋Š” .csv์ด๋ฉฐ, ์—‘์…€๊ณผ ์œ ์‚ฌํ•œ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง„๋‹ค.
์‹ค์ œ ๊ณต๊ณต๋ฐ์ดํ„ฐํฌํ„ธ์— ๋“ค์–ด๊ฐ€๋ณด๋ฉด, Open API๊ฐ€ ์žˆ๋Š”๋ฐ apiํ˜•์‹์œผ๋กœ โ€˜json, xml, csvโ€™์™€ ๊ฐ™์€ ํ˜•์‹์ด ์กด์žฌํ•œ๋‹ค.

์—ฐ๋„,์ œ์กฐ์‚ฌ,๋ชจ๋ธ,์„ค๋ช…,๊ฐ€๊ฒฉ
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!air, moon roof, loaded",4799.00

ํŒŒ์‹ฑ์ด๋ž€?

ํŒŒ์‹ฑ(Parsing)์ด๋ž€, ๋ฌธ์ž์—ด์—์„œ ๋„ค์ดํ‹ฐ๋ธŒ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋™์ž‘์„ ๋งํ•œ๋‹ค.
์‰ฝ๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด โ€œ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์•„๋‚ธ๋‹คโ€๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

Key 1๊ฐœ:Value 1๊ฐœ ํŒŒ์‹ฑ

ํŒŒ์ด์ฌ ๊ธฐ์ค€์œผ๋กœ, Key 1๊ฐœ:Value 1๊ฐœ๋ฅผ ํŒŒ์‹ฑํ•ด๋ณด์ž.

js1 = '{"id": 1, "name": "helloalpaca"}'  
jsonObject = json.loads(js1)
print(jsonObject.get("name"))

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๊ฒฐ๊ณผ ๊ฐ’์œผ๋กœ โ€œhelloalpacaโ€๊ฐ€ ์ถœ๋ ฅ๋  ๊ฒƒ์ด๋‹ค.

Key 1๊ฐœ:Value N๊ฐœ ํŒŒ์‹ฑ

๋‹ค์Œ์œผ๋กœ Key 1๊ฐœ:Value N๊ฐœ๋ฅผ ํŒŒ์‹ฑํ•ด๋ณด์ž.

js2 = '{"id": 1, "name": ["helloalpaca", "choppermask"]}'
jsonObject = json.loads(js2)
jsonArray = jsonObject.get("name")
for list in jsonArray:
    print(list)

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๊ฒฐ๊ณผ ๊ฐ’์œผ๋กœ โ€œhelloalpaca, choppermaskโ€๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

์ค‘์ฒฉ๋œ Object ํŒŒ์‹ฑ

info๋ผ๋Š” key์˜ ์ •๋ณด๋ฅผ get()์„ ์ค‘์ฒฉ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์‹ฑํ•ด๋ณด์ž.

js3 = '{"id": 1, "info": {"name": "helloalpaca", "email": "jms393497@gmail.com"}}'
jsonObject = json.loads(js3)
print(jsonObject.get("info").get("name"))

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด โ€œhelloalpacaโ€๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

JSON ํŒŒ์ผ ์ฝ๊ธฐ

jsonํŒŒ์ผ์„ ์ฝ์–ด์˜ค๊ณ  ์‹ถ์œผ๋ฉด, open()์— ๋‚ด๊ฐ€ ์ฝ์„ json ํŒŒ์ผ์„ ์ง€์ •ํ•ด์ฃผ๊ณ , laod()๋กœ ๊ฐ€์ ธ์˜ค๋ฉด ๋œ๋‹ค.

import json  
file = open('example.json')
jsonString = json.load(file)
print(jsonString.get("info"))

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ