IOS 프로그래밍

본문 바로가기
사이트 내 전체검색


IOS 프로그래밍
IOS 프로그래밍

8. JSON 파싱

페이지 정보

작성자 관리자 댓글 0건 조회 3,167회 작성일 20-02-11 08:20

본문

8. JSON 파싱

import Foundation

let data: Data // received from a network request, for example
let json = try? JSONSerialization.jsonObject(with: data, options: [])

 

// Example JSON with object root:
/*
{
"someKey": 42.0,
"anotherKey": {
"someNestedKey": true
}
}
*/
if let dictionary = jsonWithObjectRoot as? [String: Any] {
if let number = dictionary["someKey"] as? Double {
// access individual value in dictionary
}

for (key, value) in dictionary {
// access all key / value pairs in dictionary
}

if let nestedDictionary = dictionary["anotherKey"] as? [String: Any] {
// access nested dictionary values by key
}
}

// Example JSON with array root:
/*
[
"hello", 3, true
]
*/
if let array = jsonWithArrayRoot as? [Any] {
if let firstObject = array.first {
// access individual object in array
}

for object in array {
// access all objects in array
}

for case let string as String in array {
// access only string values in array
}
}

 

 

참고

 

https://developer.apple.com/swift/blog/?id=37

댓글목록

등록된 댓글이 없습니다.


개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 063-469-4551 FAX. 063-469-4560 전북 군산시 대학로 558
군산대학교 컴퓨터정보공학과

Copyright © www.leelab.co.kr. All rights reserved.