(Javascript Object Notation)
โข Javascript ์ธ์ด๋ก๋ถํฐ ํ์ (JavaScript Object Notation)
โข ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ํ๋ซํผ ๊ฐ ๋ ๋ฆฝ์ ์ด๊ณ ๊ฐ๋ฒผ์์ XML ๋ฐฉ์์ ๋์ฒดํ์ฌ ํ์ฌ ๊ฑฐ์ ํ์ค์ผ๋ก ์ฌ์ฉ๋๊ณ ์๋ ๋ฐ์ดํฐ ๊ตํ ํ์
โข ๋ ๊ฐ์ ๊ตฌ์กฐ๋ฅผ ๊ธฐ๋ณธ์ผ๋ก ๊ฐ์ง
- 'Name : Value' ํํ์ ์์ ์ด๋ฃจ๋ ์ฝ๋ ์ ํ์ . ๊ฐ ์ธ์ด์์ Hash table, Dictionary ๋ฑ์ผ๋ก ๊ตฌํ
- ๊ฐ๋ค์ ์์ํ๋ ๋ฆฌ์คํธ. ๋๋ถ๋ถ์ ์ธ์ด๋ค์์ Array, Vector, List ๋๋ Sequence ๋ก ๊ตฌํ
โข XML ์ ๋นํด ๊ธฐ๋ฅ์ด ์ ๊ณ ๊ตฌ์กฐ๊ฐ ๋จ์ํ์ฌ ํ์ฑ์ด ์ฝ๊ณ ๋น ๋ฅด๋ฉฐ ์ ์ ์ฉ๋์ผ๋ก ์ ์ฅ ๊ฐ๋ฅ ๋ฐ๋ผ์ ์ฌ๋์ด ์ฝ๊ณ ์ฐ๋ ๊ฒ๋ฟ ์๋๋ผ ๊ธฐ๊ณ๊ฐ ๋ถ์ํ๊ณ ์์ฑํ๋ ๊ฒ์๋ (์๋์ ์ผ๋ก) ๋ ์ฉ์ด
โข contents type ์ application/json ์ด๋ฉฐ, ํ์ผ ํ์ฅ์๋ .json, ๊ธฐ๋ณธ ์ธ์ฝ๋ฉ์ UTF-8 ์ ์ฌ์ฉ
- ๋์ ๋๋ฆฌ์ ์์๊ณผ ๋์ด ๋ค๋ฆ.
- key value๋ก ๊ตฌ์ฑ
(๋ฌธ์์ด์ ๋ฐ์ดํฐ๋ก ๋ณํํด์, JSON์ ๋ค์ swift์์ ๋ค๋ฃฐ ์ ์๋ ๋ฐฐ์ด๋ก ๋ฐํ๋ฑ..)
- JSONSerialization (swift 4์ด์๊ฐ๋ฅ)
- ํ์ฑํ๊ธฐ
๊ธฐ๋ณธํ
let jsonData1 = """
{ //๋์
๋๋ฆฌ
"greeting": "hello world",
"foo": "bar",
"iOS": "Swift"
}
""".data(using: .utf8)!
//1. ์์ json์ ๋ง๋ค์ด์ค.
do { //2.JSONSerialization// 3.try: ์๋ฌ์ฒ๋ฆฌํ ์ ์๋๋ก. (ํน์ ์ ๋ฐ์ดํฐ๊ฐ์ด ์๋ชป๋ ๊ฒฝ์ฐ๋ฅผ ๋๋น)
if let json = try JSONSerialization.jsonObject(with: jsonData1) as? [String: String],
let greeting = json["greeting"],
let foo = json["foo"],
let iOS = json["iOS"] {
print(greeting)
print(foo)
print(iOS)
}
} catch {
print(error)
}
//4.ํ์
์ด ์๋ชป๋ ๊ฒฝ์ฐ ์ข
๋ฃ๋จ. [String: String]
- throws(๋ด๊ฐ ์ค๋ฅ์ฒ๋ฆฌ ์ํ๊ณ ๋ค๊ฐ ํด.)๊ฐ ์๋ ๊ฒฝ์ฐ try๋ฅผ ์ฌ์ฉํจ. (ํจ์ ์์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊ฐ๋ฅ์ฑ์ด ์๋ ๊ฒฝ์ฐ, ๋ฐ๊นฅ์ผ๋ก )
- ๋ง์ฝ, ์ด ๊ณผ์ ์ด ์คํจํ๋ฉด catch
- ์์ฐ๊ฒ ๋ ๊ฒฝ์ฐ, Fix ๋ธ. (๊ฒฐ๊ตญ, ์ค๋ฅ์ฒ๋ฆฌ๋ฅผ ์ํด ํด์ผํ๋ ๊ณผ์ .)
์๋ก ๋ฐ์ดํฐ ํ์ ์ด ๋ค๋ฅธ ๊ฒฝ์ฐ.
let jsonData2 = """
{
"greeting": "hello world",
"iOS": true,
"SwiftVersion": 5
}
""".data(using: .utf8)!
//1.์์ ๋ด์ฉ json๋ง๋ฆ.
if let json = try? JSONSerialization.jsonObject(with: jsonData2) as? [String: Any], //2.Any:๋ชจ๋ ํ์
๊ฐ๋ฅ
let greeting = json["greeting"] as? String, //(๊ฐ๊ฐ, ๊ฐ์ ๊บผ๋ผ ๋) 3.์ ๋ํ์
์ด์๊ธฐ ๋๋ฌธ์, ๊ฐ๊ฐ ์ํ๋ ํน์ ํ์
์ผ๋ก ํ์
์บ์คํ
๋ฐ๋ก ํด์ค์ผํจ.
let iOS = json["iOS"] as? Bool,
let version = json["SwiftVersion"] as? Int {
print(greeting)
print(iOS)
print(version)
}
- [์๋ฌํธ๋ค๋ง]
- do catch ๋ try ์จ์ผํจ.
- try? ๋
do catch๋ฅผ ์ฐ์ง ์์ ๊ฒฝ์ฐ. try? ์ผ ๋ ์คํจํ๋ฉด nil, - try! ์ผ ๋ ์คํจํ๋ฉด ์ฑ ์ข ๋ฃ.
๋์ ๋๋ฆฌ๊ฐ ๋ฐฐ์ด ์์ ๋ค์ด๊ฐ ๊ฒฝ์ฐ
let jsonData3 = """
[
{
"postID": 1,
"title": "JSON",
"body": "Javascript Object Notation"
},
{
"postID": 2,
"title": "JSON ํ์ฑ",
"body": "JSONSerialization์ ์ด์ฉํ ๋ฐฉ๋ฒ"
}
]
""".data(using: .utf8)!
//1.Post ์ค์ํํธ์์ ๋ค๋ฃจ๊ธฐ ์ํด ๋ชจ๋ธ ๋ณํ.
struct Post {
let postID: Int
let title: String
let body: String
}
//2. try
if let jsonObjects = try? JSONSerialization.jsonObject(with: jsonData3) as? [[String: Any]] {
for json in jsonObjects { //3. json์ for๋ฌธ์ผ๋ก ๋๋ ค์ ์๋ฃ๊ตฌ์กฐ ๋ณํ.
if let postID = json["postID"] as? Int,
let title = json["title"] as? String,
let body = json["body"] as? String {
let post = Post(postID: postID, title: title, body: body) //4.Postํ์
์ผ๋ก ๊ฐ์ ธ์ด.
print(post)
}
}
}
Q. User ๊ตฌ์กฐ์ฒด ํ์ ์ ์ ์ธํ๊ณ , ๋ค์ JSON ํ์์ ๋ฐ์ดํฐ๋ฅผ User ํ์ ์ผ๋ก ๋ณํํ์ฌ ์ถ๋ ฅํ๊ธฐ
- JSON ๊ฒฐ๊ณผ
User(id: 1, firstName: "Robert", lastName: "Schwartz", email: "[email protected]")
User(id: 2, firstName: "Lucy", lastName: "Ballmer", email: "[email protected]")
- ๋ฌธ์
// ํํธ: TopLevel์ ํ์
- Dictionary
let userJSONData = """
{
"users": [
{
"id": 1,
"first_name": "Robert",
"last_name": "Schwartz",
"email": "[email protected]"
},
{
"id": 2,
"first_name": "Lucy",
"last_name": "Ballmer",
"email": "[email protected]"
},
]
}
""".data(using: .utf8)!
- ํ์ด
let userJSONData = """
{
"users": [
{
"id": 1,
"first_name": "Robert",
"last_name": "Schwartz",
"email": "[email protected]"
},
{
"id": 2,
"first_name": "Lucy",
"last_name": "Ballmer",
"email": "[email protected]"
},
]
}
""".data(using: .utf8)!
struct User {
let id: Int
let firstName: String
let lastName: String
let email: String
//init?: ์์ฑ์์์ ์์ฑ์ด ์คํจํ ๊ฐ๋ฅ์ฑ์ด์๋๊ฒฝ์ฐ ?๋ฅผ ๋ถ์.
init?(from json: [String: Any]) { //json๋ฐ์ดํฐ ๋๊ฒจ๋ฐ์.
guard let id = json["id"] as? Int,
let firstName = json["first_name"] as? String,
let lastName = json["last_name"] as? String,
let email = json["email"] as? String
else { print("Parsing error"); return nil }
self.id = id
self.firstName = firstName
self.lastName = lastName
self.email = email
}
}
func answer(jsonData: Data) {
guard let json = try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any],
let userList = json["users"] as? [[String: Any]]
else { return }
//compactMap: ํน์ ์คํจํ ๊ฒฝ์ฐ nil์ด๋ผ๋ ์ ๋๋ก๋ ๊ฐ์ ๋ฐ๊ธฐ์ํด ์.
let users: [User] = userList.compactMap { User(from: $0) } //User(from: $0)๋ users์ ๊ฐ์ ๊ฒ.
users.forEach { print($0) }
}
answer(jsonData: userJSONData)