swift create json and parse
iOS 2015. 7. 21. 19:12https://medium.com/swift-programming/4-json-in-swift-144bf5f88ce4
import Foundation
print("Hello, World!")
let jsonObject: [AnyObject] = [
["name": "John", "age": 21],
["name": "Bob", "age": 35],
]
print( jsonObject )
func JSONStringify( value: AnyObject, prettyPrinted:Bool = false) -> String{
let options = prettyPrinted ? NSJSONWritingOptions.PrettyPrinted : NSJSONWritingOptions(rawValue: 0)
if NSJSONSerialization.isValidJSONObject(value){
do{
let data = try NSJSONSerialization.dataWithJSONObject(value, options: options)
if let string = NSString(data: data, encoding: NSUTF8StringEncoding){
return string as String
}
}catch{
print("error")
}
}
return ""
}
print(JSONStringify(jsonObject, prettyPrinted: true));
-output-
Hello, World!
[{
age = 21;
name = John;
}, {
age = 35;
name = Bob;
}]
[
{
"age" : 21,
"name" : "John"
},
{
"age" : 35,
"name" : "Bob"
}
]
Program ended with exit code: 0
'iOS' 카테고리의 다른 글
[swift] card (0) | 2015.07.23 |
---|---|
[swift] protocol adjusted class, structure and enum (0) | 2015.07.23 |
IOS 개발팁 (32BIT로 개발된 앱을 64BIT로 변환 방법) (0) | 2015.07.13 |
xcode Home, End key (0) | 2015.07.12 |
used as the name of the previous parameter rather than as part of the selector (0) | 2015.07.12 |