当前位置: > > > Swift - 解析JSON数据(内置JSONSerialization与第三方JSONKit)

Swift - 解析JSON数据(内置JSONSerialization与第三方JSONKit)

一,使用自带的JSONSerialization
苹果从IOS5.0后推出了SDK自带的JSON解决方案NSJSONSerialization。而自Swift3起,这个又改名成JSONSerialization。这是一个非常好用的JSON生成和解析工具,效率也比其他第三方开源项目高。

JSONSerialization能将JSON转换成Foundation对象,也能将Foundation对象转换成JSON,但转换成JSON的对象必须具有如下属性:
1,顶层对象必须是Array或者Dictionary
2,所有的对象必须是String、Number、Array、Dictionary、Null的实例
3,所有Dictionary的key必须是String类型
4,数字对象不能是非数值或无穷
注意:尽量使用JSONSerialization.isValidJSONObject先判断能否转换成功。

样例1:将对象转成json字符串,再转回来
import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let label = UILabel(frame:CGRect(x:100, y:100, width:300, height:100))
        label.text = "输出结果在控制台"
        self.view.addSubview(label)
        //测试结果在output终端输入,也可以建个命令行应用测试就可以了
        testJson()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    //测试json
    func testJson() {
        //Swift对象
        let user:[String: Any] = [
            "uname": "张三",
            "tel": ["mobile": "138", "home": "010"]
        ]
        //首先判断能不能转换
        if (!JSONSerialization.isValidJSONObject(user)) {
            print("is not a valid json object")
            return
        }
        //利用自带的json库转换成Data
        //如果设置options为JSONSerialization.WritingOptions.prettyPrinted,则打印格式更好阅读
        let data = try? JSONSerialization.data(withJSONObject: user, options: [])
        //Data转换成String打印输出
        let str = String(data:data!, encoding: String.Encoding.utf8)
        //输出json字符串
        print("Json Str:"); print(str)
        
        //把Data对象转换回JSON对象
        let json = try? JSONSerialization.jsonObject(with: data!,
                                            options:.allowFragments) as! [String: Any]
        print("Json Object:", json)
        //验证JSON对象可用性
        let uname = json?["uname"]
        let mobile = (json?["tel"] as! [String: Any])["mobile"]
        print("get Json Object:","uname: \(uname), mobile: \(mobile)")
    }
}
输出结果如下:
Json Str:
Optional({"uname":"张三","tel":{"home":"010","mobile":"138"}})
Json Object:
{
    tel =     {
        home = 010;
        mobile = 138;
    };
    uname = "\U5f20\U4e09";
}
get Json Object:
uname: 张三, mobile: 138

样例2:解析json字符串
(由于是字符串内容是json数组,则转成NSArray。如果字符串是json对象,则转成NSDictionary。)
let string = "[{\"ID\":1,\"Name\":\"元台禅寺\",\"LineID\":1},{\"ID\":2,\"Name\":\"田坞里山塘\",\"LineID\":1},{\"ID\":3,\"Name\":\"滴水石\",\"LineID\":1}]"
let data = string.data(using: String.Encoding.utf8)

let jsonArr = try! JSONSerialization.jsonObject(with: data!,
options: JSONSerialization.ReadingOptions.mutableContainers) as! [[String: Any]]

print("记录数:\(jsonArr.count)")
for json in jsonArr {
    print("ID:", json["ID"]!, "    Name:", json["Name"]!)
}
控制台输出如下:
  

二,使用第三方库 - JSONKit 
(JSONKit已经有多年不更新了,而且是使用OC写的。所以不推荐使用,第三方的话建议用下面介绍的SwiftyJSON)
1,新建桥街头文件Bridging-Header.h,并设置到编译参数里
#include "JSONKit.h"
2,将JSONKit的库文件导入到项目中来(JSONKit.h和JSONKit.m)
GitHub主页地址:https://github.com/johnezang/JSONKit
  
3,这时编译会发现报错,这是由于JSONKit库不支持Objective-C的自动引用计数功能导致。
需要在Build Phases -> Compile Sources -> JSONKit.m,双击添加Comipler Flag:-fno-objc-arc 。这样就完成了不支持自动引用计数的配置。
 

4,可能这时还是编译报错,这是由于库代码写的比较早。isa方法现在已经被废除。点击错误选择“Fix-it”自动修复下即可。


测试代码:
import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        testJson()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    func testJson() {
        //Swift 字典对象
        let user:[String: Any] = [
            "uname": "user1",
            "tel": ["mobile": "138", "home": "010"]
        ]
        //使用 JSONKit 转换成为 JSON 字符串
        let jsonstring = (user as NSDictionary).jsonString()
        print(jsonstring);
        //由字符串反解析回字典
        print(jsonstring?.objectFromJSONString() as! NSDictionary)
        
        //使用 JSONKit 转换成为 NSData 类型的 JSON 数据
        let jsondata = (user as NSDictionary).jsonData() as NSData
        print(jsondata);
        //由NSData 反解析回为字典
        print(jsondata.objectFromJSONData() as! NSDictionary)
    }
}
输出结果:
{"uname":"user1","tel":{"home":"010","mobile":"138"}}
{
    tel =     {
        home = 010;
        mobile = 138;
    };
    uname = user1;
}
<7b22756e 616d6522 3a227573 65723122 2c227465 6c223a7b 22686f6d 65223a22 30313022 2c226d6f 62696c65 223a2231 3338227d 7d>
{
    tel =     {
        home = 010;
        mobile = 138;
    };
    uname = user1;
}
源码下载hangge_647.zip

三,使用第三方库 - SwiftyJSON(推荐) 
SwiftyJSON是个使用Swift语言编写的开源库,可以让我们很方便地处理JSON数据(解析数据、生成数据)
具体使用方法可以看我的这篇文章:http://www.hangge.com/blog/cache/detail_968.html

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作。

打赏支持
评论6
  • 6楼
    2016-07-20 14:32
    daimaperson

    航哥,我解析完json之后,想把数据放在model里面,出现了NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x10d515d80'这个异常,怎么解决

    站长回复

    看你这个异常信息我也不确定是什么原因,只能靠你自己结合代码来定位了。

  • 5楼
    2015-12-16 16:52
    niltonil

    这个是我发布的webservice的数据
    <?xml version="1.0" encoding="UTF-8"?>
    <string xmlns="http://tempuri.org/">[{"ID":1,"Name":"元台禅寺","Url":"spotPic/元台禅寺.jpg","LineID":1},{"ID":2,"Name":"田坞里山塘","Url":"spotPic/田坞里山塘.jpg","LineID":1},{"ID":3,"Name":"滴水石","Url":"spotPic/滴水石","LineID":1},{"ID":4,"Name":"青蛙石","Url":"spotPic/青蛙石","LineID":1},{"ID":5,"Name":"平安石","Url":"spotPic/平安石","LineID":1},{"ID":6,"Name":"小五桥","Url":"spotPic/小五桥","LineID":2},{"ID":7,"Name":"大林口子","Url":"spotPic/大林口子","LineID":2},{"ID":8,"Name":"跳断廊","Url":"spotPic/跳断廊","LineID":1},{"ID":9,"Name":"梅子树","Url":"spotPic/梅子树","LineID":1},{"ID":10,"Name":"300年古树","Url":"spotPic/300年古树","LineID":1},{"ID":11,"Name":"林海亭","Url":"spotPic/林海亭","LineID":1},{"ID":12,"Name":"岩山景区","Url":"spotPic/岩山景区","LineID":1},{"ID":13,"Name":"石岗头","Url":"spotPic/石岗头","LineID":1},{"ID":14,"Name":"佛堂村石碑","Url":"spotPic/佛堂村石碑","LineID":2},{"ID":15,"Name":"小瀑布","Url":"spotPic/小瀑布","LineID":1},{"ID":16,"Name":"一线天","Url":"spotPic/一线天","LineID":2},{"ID":17,"Name":"大斗坞水库","Url":"spotPic/大斗坞水库","LineID":1},{"ID":18,"Name":"老鹰石观景点","Url":"spotPic/老鹰石观景点","LineID":2},{"ID":19,"Name":"仙人洞","Url":"spotPic/仙人洞","LineID":1},{"ID":20,"Name":"山顶观景点","Url":"spotPic/山顶观景点","LineID":1},{"ID":21,"Name":"红旗山塘","Url":"spotPic/红旗山塘","LineID":2}]</string>
    通过NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary解析得到
    {
    d = "[{\"ID\":1,\"Name\":\"\U5143\U53f0\U7985\U5bfa\",\"Url\":\"spotPic/\U5143\U53f0\U7985\U5bfa.jpg\",\"LineID\":1},{\"ID\":2,\"Name\":\"\U7530\U575e\U91cc\U5c71\U5858\",\"Url\":\"spotPic/\U7530\U575e\U91cc\U5c71\U5858.jpg\",\"LineID\":1},{\"ID\":3,\"Name\":\"\U6ef4\U6c34\U77f3\",\"Url\":\"spotPic/\U6ef4\U6c34\U77f3\",\"LineID\":1},{\"ID\":4,\"Name\":\"\U9752\U86d9\U77f3\",\"Url\":\"spotPic/\U9752\U86d9\U77f3\",\"LineID\":1},{\"ID\":5,\"Name\":\"\U5e73\U5b89\U77f3\",\"Url\":\"spotPic/\U5e73\U5b89\U77f3\",\"LineID\":1},{\"ID\":6,\"Name\":\"\U5c0f\U4e94\U6865\",\"Url\":\"spotPic/\U5c0f\U4e94\U6865\",\"LineID\":2},{\"ID\":7,\"Name\":\"\U5927\U6797\U53e3\U5b50\",\"Url\":\"spotPic/\U5927\U6797\U53e3\U5b50\",\"LineID\":2},{\"ID\":8,\"Name\":\"\U8df3\U65ad\U5eca\",\"Url\":\"spotPic/\U8df3\U65ad\U5eca\",\"LineID\":1},{\"ID\":9,\"Name\":\"\U6885\U5b50\U6811\",\"Url\":\"spotPic/\U6885\U5b50\U6811\",\"LineID\":1},{\"ID\":10,\"Name\":\"300\U5e74\U53e4\U6811\",\"Url\":\"spotPic/300\U5e74\U53e4\U6811\",\"LineID\":1},{\"ID\":11,\"Name\":\"\U6797\U6d77\U4ead\",\"Url\":\"spotPic/\U6797\U6d77\U4ead\",\"LineID\":1},{\"ID\":12,\"Name\":\"\U5ca9\U5c71\U666f\U533a\",\"Url\":\"spotPic/\U5ca9\U5c71\U666f\U533a\",\"LineID\":1},{\"ID\":13,\"Name\":\"\U77f3\U5c97\U5934\",\"Url\":\"spotPic/\U77f3\U5c97\U5934\",\"LineID\":1},{\"ID\":14,\"Name\":\"\U4f5b\U5802\U6751\U77f3\U7891\",\"Url\":\"spotPic/\U4f5b\U5802\U6751\U77f3\U7891\",\"LineID\":2},{\"ID\":15,\"Name\":\"\U5c0f\U7011\U5e03\",\"Url\":\"spotPic/\U5c0f\U7011\U5e03\",\"LineID\":1},{\"ID\":16,\"Name\":\"\U4e00\U7ebf\U5929\",\"Url\":\"spotPic/\U4e00\U7ebf\U5929\",\"LineID\":2},{\"ID\":17,\"Name\":\"\U5927\U6597\U575e\U6c34\U5e93\",\"Url\":\"spotPic/\U5927\U6597\U575e\U6c34\U5e93\",\"LineID\":1},{\"ID\":18,\"Name\":\"\U8001\U9e70\U77f3\U89c2\U666f\U70b9\",\"Url\":\"spotPic/\U8001\U9e70\U77f3\U89c2\U666f\U70b9\",\"LineID\":2},{\"ID\":19,\"Name\":\"\U4ed9\U4eba\U6d1e\",\"Url\":\"spotPic/\U4ed9\U4eba\U6d1e\",\"LineID\":1},{\"ID\":20,\"Name\":\"\U5c71\U9876\U89c2\U666f\U70b9\",\"Url\":\"spotPic/\U5c71\U9876\U89c2\U666f\U70b9\",\"LineID\":1},{\"ID\":21,\"Name\":\"\U7ea2\U65d7\U5c71\U5858\",\"Url\":\"spotPic/\U7ea2\U65d7\U5c71\U5858\",\"LineID\":2}]";
    }
    let a = json.objectForKey("d") print(a)得到
    Optional([{"ID":1,"Name":"元台禅寺","Url":"spotPic/元台禅寺.jpg","LineID":1},{"ID":2,"Name":"田坞里山塘","Url":"spotPic/田坞里山塘.jpg","LineID":1},{"ID":3,"Name":"滴水石","Url":"spotPic/滴水石","LineID":1},{"ID":4,"Name":"青蛙石","Url":"spotPic/青蛙石","LineID":1},{"ID":5,"Name":"平安石","Url":"spotPic/平安石","LineID":1},{"ID":6,"Name":"小五桥","Url":"spotPic/小五桥","LineID":2},{"ID":7,"Name":"大林口子","Url":"spotPic/大林口子","LineID":2},{"ID":8,"Name":"跳断廊","Url":"spotPic/跳断廊","LineID":1},{"ID":9,"Name":"梅子树","Url":"spotPic/梅子树","LineID":1},{"ID":10,"Name":"300年古树","Url":"spotPic/300年古树","LineID":1},{"ID":11,"Name":"林海亭","Url":"spotPic/林海亭","LineID":1},{"ID":12,"Name":"岩山景区","Url":"spotPic/岩山景区","LineID":1},{"ID":13,"Name":"石岗头","Url":"spotPic/石岗头","LineID":1},{"ID":14,"Name":"佛堂村石碑","Url":"spotPic/佛堂村石碑","LineID":2},{"ID":15,"Name":"小瀑布","Url":"spotPic/小瀑布","LineID":1},{"ID":16,"Name":"一线天","Url":"spotPic/一线天","LineID":2},{"ID":17,"Name":"大斗坞水库","Url":"spotPic/大斗坞水库","LineID":1},{"ID":18,"Name":"老鹰石观景点","Url":"spotPic/老鹰石观景点","LineID":2},{"ID":19,"Name":"仙人洞","Url":"spotPic/仙人洞","LineID":1},{"ID":20,"Name":"山顶观景点","Url":"spotPic/山顶观景点","LineID":1},{"ID":21,"Name":"红旗山塘","Url":"spotPic/红旗山塘","LineID":2}])

    站长回复

    明白了。不过很奇怪为什么你解析出来会有“d = ”这个东西。看原数据里应该是json数组,只要解析成NSArray即可(见我文章新加的样例2)

  • 4楼
    2015-12-15 17:55
    niltonil

    您好 我想问一下 如果我的结果是这样的
    {
    d=[{"id":1,"name":2},{"id":2,"name":3},{"id":3,"name":2},{"id":4,"name":2},{"id":5,"name":2}]
    }应该怎么解析呢? 谢谢

    站长回复

    这个是你自己拼接的数据吗,因为我看这个不是一个规范的JSON数据格式,所以使用JSON工具没法解析。

  • 3楼
    2015-12-15 09:50
    action

    站长你好,我用swift写的SQLIte,插入了数据,然后把app删了,再次运行在虚拟机上,数据库里的文件就没了,这个问题是什么原因啊,还有就是要怎么保存呢

    站长回复

    由于iOS的沙盒机制,用户数据文件(包括sqlite数据库文件)和程序是在一起的。如果把APP删除了,这些文件自然也不存在了。如果想要保留,就不能只把数据在本地保存,可以考虑使用iCloud进行数据同步。

  • 2楼
    2015-11-11 19:13
    nameDC

    感谢,望继续更新

    站长回复

    我会持续更新的,欢迎常来看看。

  • 1楼
    2015-10-26 12:17
    WANGTZ

    航哥,Swift2.0语法有更新,在Xcode7中运行文章实例,需要修改:(1)println改成了print(2)dataWithJSONObject参数有调整:NSJSONSerialization.dataWithJSONObject(user, options: nil, error: nil) 需要改成 try? NSJSONSerialization.dataWithJSONObject(user, options: []) .多谢航哥的文章!

    站长回复

    感谢提醒,现已修改。如果发现其他问题,可以再给我留言。