Skip to content

Latest commit

ย 

History

History
243 lines (186 loc) ยท 5.95 KB

JSON.md

File metadata and controls

243 lines (186 loc) ยท 5.95 KB

JSON

(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 ์„ ์‚ฌ์šฉ

JSON Format Example

  • ๋”•์…”๋„ˆ๋ฆฌ์˜ ์‹œ์ž‘๊ณผ ๋์ด ๋‹ค๋ฆ„.
  • key value๋กœ ๊ตฌ์„ฑ

JSON va XML

JSON in Swift

(๋ฌธ์ž์—ด์„ ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜ํ•ด์„œ, JSON์„ ๋‹ค์‹œ swift์—์„œ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜๋“ฑ..)

JSON Bascie

  • JSONSerialization (swift 4์ด์ƒ๊ฐ€๋Šฅ)
  • ํŒŒ์‹ฑํ•˜๊ธฐ

JSON Parsing (1)

๊ธฐ๋ณธํ˜•

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 ๋œธ. (๊ฒฐ๊ตญ, ์˜ค๋ฅ˜์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ํ•ด์•ผํ•˜๋Š” ๊ณผ์ •.)

JSON Parsing (2)

์„œ๋กœ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ.

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! ์ผ ๋•Œ ์‹คํŒจํ•˜๋ฉด ์•ฑ ์ข…๋ฃŒ.

JSON Parsing (3)

๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ๋ฐฐ์—ด ์•ˆ์— ๋“ค์–ด๊ฐ„ ๊ฒฝ์šฐ

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)