json 读写 swift

时间:2023-03-09 01:42:23
json 读写 swift

//

// ViewController.swift

// json读写

//

// Created by mac on 15/7/14.

// Copyright (c) 2015年 fangyuhao. All rights reserved.

//

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var json: AnyObject? = NSJSONSerialization.JSONObjectWithData(NSData(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("obj", ofType: "json")!)!)!, options: NSJSONReadingOptions.allZeros, error: nil) if let lang: AnyObject = json?.objectForKey("language") {
println(lang)
}
//生成
var dict = ["name":"jikexueyuan","age":1]
var jsonData = NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions.allZeros, error: nil)
if let str = NSString(data: jsonData!, encoding: NSUTF8StringEncoding){
println(str)
} } override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}