Swift - 解析JSON数据(内置NSJSONSerialization与第三方JSONKit)
一,使用自带的NSJSONSerialization
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import UIKit class ViewController : UIViewController { override func viewDidLoad() { super .viewDidLoad() // Do any additional setup after loading the view, typically from a nib. var label: UILabel = UILabel (frame: CGRectMake (100, 100,300,100)); label.text = "输出结果在控制台" self .view.addSubview(label) //测试结果在output终端输入,也可以建个命令行应用测试就可以了 testJson() } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //测试json func testJson() { //Swift对象 let user = [ "uname" : "张三" , "tel" : [ "mobile" : "138" , "home" : "010" ] ] //首先判断能不能转换 if (! NSJSONSerialization .isValidJSONObject(user)) { println ( "is not a valid json object" ) return } //利用OC的json库转换成OC的NSData, //如果设置options为NSJSONWritingOptions.PrettyPrinted,则打印格式更好阅读 let data : NSData ! = NSJSONSerialization .dataWithJSONObject(user, options: nil , error: nil ) //NSData转换成NSString打印输出 let str = NSString (data:data, encoding: NSUTF8StringEncoding ) //输出json字符串 println ( "Json Str:" ); println (str) //把NSData对象转换回JSON对象 let json : AnyObject ! = NSJSONSerialization . JSONObjectWithData (data, options: NSJSONReadingOptions . AllowFragments , error: nil ) println ( "Json Object:" ); println (json) //验证JSON对象可用性 let uname : AnyObject = json.objectForKey( "uname" )! let mobile : AnyObject = json.objectForKey( "tel" )!.objectForKey( "mobile" )! println ( "get Json Object:" ); println ( "uname: \(uname), mobile: \(mobile)" ) } } |
输出结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
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 |
二,使用第三方库 - JSONKit
1
|
# include "JSONKit.h" |
2,将JSONKit的库文件导入到项目中来(JSONKit.h和JSONKit.m)
![](http://www.hangge.com/blog_uploads/201503/2015030616162021299.png)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import UIKit class ViewController : UIViewController { override func viewDidLoad() { super .viewDidLoad() // Do any additional setup after loading the view, typically from a nib. testJson() } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func testJson() { //Swift 字典对象 let user = [ "uname" : "user1" , "tel" : [ "mobile" : "138" , "home" : "010" ] ] //使用 JSONKit 转换成为 JSON 字符串 var jsonstring = (user as NSDictionary ). JSONString () println (jsonstring); //由字符串反解析回字典 println (jsonstring.objectFromJSONString() as NSDictionary ) //使用 JSONKit 转换成为 NSData 类型的 JSON 数据 var jsondata = (user as NSDictionary ). JSONData () println (jsondata); //由NSData 反解析回为字典 println (jsondata.objectFromJSONData() as NSDictionary ) } } |
输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
{ "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; } |
Swift - 解析JSON数据(内置NSJSONSerialization与第三方JSONKit)的更多相关文章
- IOS 解析Json数据(NSJSONSerialization)
● 什么是JSON ● JSON是一种轻量级的数据格式,一般用于数据交互 ● 服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除 外) ● JSON的格式很像OC中的字典和数组 ...
- Android网络之数据解析----使用Google Gson解析Json数据
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- 教你不编程快速解析 JSON 数据
JSON 是一种轻量级的,不受语言约束的数据存储格式,大部分编程语言都可以解析它,并且对编程人员也十分友好.我们在进行通讯/数据交互时,非常经常用到 JSON 格式. 但是,我们在进行数据存储的时候, ...
- 使用jQuery解析JSON数据
我们先以解析上例中的comments对象的JSON数据为例,然后再小结jQuery中解析JSON数据的方法. 上例中得到的JSON数据如下,是一个嵌套JSON: {"comments&quo ...
- [转]javascript eval函数解析json数据时为什加上圆括号eval("("+data+")")
javascript eval函数解析json数据时为什么 加上圆括号?为什么要 eval这里要添加 “("("+data+")");//”呢? 原因在于: ...
- 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
摘自:http://blog.csdn.net/mazhaojuan/article/details/8592015 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来 ...
- 使用jQuery解析JSON数据(由ajax发送请求到php文件处理数据返回json数据,然后解析json写入html中呈现)
在上一篇的Struts2之ajax初析中,我们得到了comments对象的JSON数据,在本篇中,我们将使用jQuery进行数据解析. 我们先以解析上例中的comments对象的JSON数据为例,然后 ...
- (转)springMVC框架下JQuery传递并解析Json数据
springMVC框架下JQuery传递并解析Json数据 json作为一种轻量级的数据交换格式,在前后台数据交换中占据着非常重要的地位.Json的语法非常简单,采用的是键值对表示形式.JSON 可以 ...
- Delphi中使用ISuperObject解析Json数据
Java.Php等语言中都有成熟的框架来解析Json数据,可以让我们使用很少的代码就把格式化好的json数据转换成程序可识别的对象或者属性,同时delphi中也有这样的组件来实现此功能,即Isuper ...
随机推荐
- ZOJ 3594 年份水题 【注意:没有0年】
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...
- Agg vs. Cairo 二维绘图引擎之比较和选择 .
Agg vs. Cairo 二维绘图引擎之比较和选择 cheungmine 当今时代对于作为二维图形软件开发者, 是幸运的.因为除了Windows GDI/GDI+之外,我们还有很多其他的选择.而且这 ...
- MVC客户端验证的小示例
MVC客户端验证的小示例 配置客户端验证的可用性: <configuration> <appSettings> <add key="ClientValidat ...
- Ch02 从零开始实例学习3
提纲:---------------------------- 演练2-3:添加控制器 知识点2-3:控制器的职责 知识点2-4:控制器的类别与方法 ------------------------- ...
- python gzip 压缩文件
压缩数据创建gzip文件 先看一个略麻烦的做法 ? 1 2 3 4 5 6 import StringIO,gzip content = 'Life is short.I use python' zb ...
- xss 跨站脚本攻击
这是一个非常简单的攻击. 两个页面如下: <form action="MyJsp.jsp" method="get"> <input type ...
- Android ListView条目全选功能,不用checkbox实现!
大家好,翻了翻曾经的笔记,发现了一个我特别标记的功能,那就是ListView全选功能,顿时想起了我那个时候苦逼的生涯,因为我大学机械出身,大学毕业了都不知道什么叫代码,在58干了一段销售.实在是干不下 ...
- Writing a Windows Shell Extension(marco cantu的博客)
Writing a Windows Shell Extension This is a technical article covering the content of my last week s ...
- Qt录音程序
源地址:http://www.oschina.net/code/snippet_1243295_48623 [代码] [C/C++]代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- Linux 安装Redis全过程日志
wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make ...