iOS开发–JSON详解手机开发

1.什么是JSON?

JSON(JavaScript Object Notation)在网络传输中几乎无处不在,JSON是一种轻量级的数据交换格式,是基于JavaScript(Standard ECMA-262 3rd Edition – December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,数据格式容易阅读和编写,同时也非常便于机器解析和生成,在网络传输速度相对于传统的XML快。JSON的英文官网http://www.json.org/,具体的格式可看官网,如果你是在看不懂,可以参考本文之前在Android中解析JSON的文章,其中还有关于JSON和XML的对比(Android数据解析-JSON解析)。

2.什么时候需要用到JSON?

2.1从服务器中请求JSON数据到客户端;

2.2从客户端上传JSON数据到服务器;

3.JSON数据;

一个最简单的JSON数据如下{“Name”:”FlyElephant”},Name是键,FlyElephant是值,用{}包裹;开发的时候经常会遇到客户端与服务端打交道,数据传输基本上都是JSON格式,一般都是服务端提供接口数据,那么我开始的时候没有数据怎么办呢,自己单独搞一个JSON字符串自己解析也可以,不过没什么意思,不过还好国家气象局提供的有接口数据,网上有些早的文章写了多个接口,个人测试只有一个接口还是可以用的。http://m.weather.com.cn/data/101180601.html,这个链接老家的天气接口,如果你想知道你老家的天气接口,把html前面的数字编码替换即可,如果想看帝都的天气,链接地址如下:http://m.weather.com.cn/data/101010100.html,下面提供了一份全国的城市编码,方便大家测试:

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
直辖市
    
“北京”
,
“上海”
,
“天津”
,
“重庆”
    
“101010100”
,
“101020100”
,
“101030100”
,
“101040100”
 
特别行政区
    
“香港”
,
“澳门”
    
“101320101”
,
“101330101”
 
黑龙江
    
“哈尔滨”
,
“齐齐哈尔”
,
“牡丹江”
,
“大庆”
,
“伊春”
,
“双鸭山”
,
“鹤岗”
,
“鸡西”
,
“佳木斯”
,
“七台河”
,
“黑河”
,
“绥化”
,
“大兴安岭”
    
“101050101”
,
“101050201”
,
“101050301”
,
“101050901”
,
“101050801”
,
“101051301”
,
“101051201”
,
“101051101”
,
“101050401”
,
“101051002”
,
“101050601”
,
“101050501”
,
“101050701”
     
吉林
    
“长春”
,
“延吉”
,
“吉林”
,
“白山”
,
“白城”
,
“四平”
,
“松原”
,
“辽源”
,
“大安”
,
“通化”
    
“101060101”
,
“101060301”
,
“101060201”
,
“101060901”
,
“101060601”
,
“101060401”
,
“101060801”
,
“101060701”
,
“101060603”
,
“101060501”
     
辽宁
    
“沈阳”
,
“大连”
,
“葫芦岛”
,
“盘锦”
,
“本溪”
,
“抚顺”
,
“铁岭”
,
“辽阳”
,
“营口”
,
“阜新”
,
“朝阳”
,
“锦州”
,
“丹东”
,
“鞍山”
    
“101070101”
,
“101070201”
,
“101071401”
,
“101071301”
,
“101070501”
,
“101070401”
,
“101071101”
,
“101071001”
,
“101070801”
,
“101070901”
,
“101071201”
,
“101070701”
,
“101070601”
,
“101070301”
     
内蒙古
    
“呼和浩特”
,
“呼伦贝尔”
,
“锡林浩特”
,
“包头”
,
“赤峰”
,
“海拉尔”
,
“乌海”
,
“鄂尔多斯”
,
“通辽”
    
“101080101”
,
“101081000”
,
“101080901”
,
“101080201”
,
“101080601”
,
“101081001”
,
“101080301”
,
“101080701”
,
“101080501”
 
河北
    
“石家庄”
,
“唐山”
,
“张家口”
,
“廊坊”
,
“邢台”
,
“邯郸”
,
“沧州”
,
“衡水”
,
“承德”
,
“保定”
,
“秦皇岛”
    
“101090101”
,
“101090501”
,
“101090301”
,
“101090601”
,
“101090901”
,
“101091001”
,
“101090701”
,
“101090801”
,
“101090402”
,
“101090201”
,
“101091101”
     
河南
    
“郑州”
,
“开封”
,
“洛阳”
,
“平顶山”
,
“焦作”
,
“鹤壁”
,
“新乡”
,
“安阳”
,
“濮阳”
,
“许昌”
,
“漯河”
,
“三门峡”
,
“南阳”
,
“商丘”
,
“信阳”
,
“周口”
,
“驻马店”
    
“101180101”
,
“101180801”
,
“101180901”
,
“101180501”
,
“101181101”
,
“101181201”
,
“101180301”
,
“101180201”
,
“101181301”
,
“101180401”
,
“101181501”
,
“101181701”
,
“101180701”
,
“101181001”
,
“101180601”
,
“101181401”
,
“101181601”
     
山东
    
“济南”
,
“青岛”
,
“淄博”
,
“威海”
,
“曲阜”
,
“临沂”
,
“烟台”
,
“枣庄”
,
“聊城”
,
“济宁”
,
“菏泽”
,
“泰安”
,
“日照”
,
“东营”
,
“德州”
,
“滨州”
,
“莱芜”
,
“潍坊”
    
“101120101”
,
“101120201”
,
“101120301”
,
“101121301”
,
“101120710”
,
“101120901”
,
“101120501”
,
“101121401”
,
“101121701”
,
“101120701”
,
“101121001”
,
“101120801”
,
“101121501”
,
“101121201”
,
“101120401”
,
“101121101”
,
“101121601”
,
“101120601”
     
山西
    
“太原”
,
“阳泉”
,
“晋城”
,
“晋中”
,
“临汾”
,
“运城”
,
“长治”
,
“朔州”
,
“忻州”
,
“大同”
,
“吕梁”
    
“101100101”
,
“101100301”
,
“101100601”
,
“101100401”
,
“101100701”
,
“101100801”
,
“101100501”
,
“101100901”
,
“101101001”
,
“101100201”
,
“101101101”
     
江苏
    
“南京”
,
“苏州”
,
“昆山”
,
“南通”
,
“太仓”
,
“吴县”
,
“徐州”
,
“宜兴”
,
“镇江”
,
“淮安”
,
“常熟”
,
“盐城”
,
“泰州”
,
“无锡”
,
“连云港”
,
“扬州”
,
“常州”
,
“宿迁”
    
“101190101”
,
“101190401”
,
“101190404”
,
“101190501”
,
“101190408”
,
“101190406”
,
“101190801”
,
“101190203”
,
“101190301”
,
“101190901”
,
“101190402”
,
“101190701”
,
“101191201”
,
“101190201”
,
“101191001”
,
“101190601”
,
“101191101”
,
“101191301”
     
安徽
    
“合肥”
,
“巢湖”
,
“蚌埠”
,
“安庆”
,
“六安”
,
“滁州”
,
“马鞍山”
,
“阜阳”
,
“宣城”
,
“铜陵”
,
“淮北”
,
“芜湖”
,
“毫州”
,
“宿州”
,
“淮南”
,
“池州”
    
“101220101”
,
“101221601”
,
“101220201”
,
“101220601”
,
“101221501”
,
“101221101”
,
“101220501”
,
“101220801”
,
“101221401”
,
“101221301”
,
“101221201”
,
“101220301”
,
“101220901”
,
“101220701”
,
“101220401”
,
“101221701”
     
陕西
    
“西安”
,
“韩城”
,
“安康”
,
“汉中”
,
“宝鸡”
,
“咸阳”
,
“榆林”
,
“渭南”
,
“商洛”
,
“铜川”
,
“延安”
    
“101110101”
,
“101110510”
,
“101110701”
,
“101110801”
,
“101110901”
,
“101110200”
,
“101110401”
,
“101110501”
,
“101110601”
,
“101111001”
,
“101110300”
     
宁夏
    
“银川”
,
“固原”
,
“中卫”
,
“石嘴山”
,
“吴忠”
    
“101170101”
,
“101170401”
,
“101170501”
,
“101170201”
,
“101170301”
     
甘肃
    
“兰州”
,
“白银”
,
“庆阳”
,
“酒泉”
,
“天水”
,
“武威”
,
“张掖”
,
“甘南”
,
“临夏”
,
“平凉”
,
“定西”
,
“金昌”
    
“101160101”
,
“101161301”
,
“101160401”
,
“101160801”
,
“101160901”
,
“101160501”
,
“101160701”
,
“101050204”
,
“101161101”
,
“101160301”
,
“101160201”
,
“101160601”
     
青海
    
“西宁”
,
“海北”
,
“海西”
,
“黄南”
,
“果洛”
,
“玉树”
,
“海东”
,
“海南”
    
“101150101”
,
“101150801”
,
“101150701”
,
“101150301”
,
“101150501”
,
“101150601”
,
“101150201”
,
“101150401”
     
湖北
    
“武汉”
,
“宜昌”
,
“黄冈”
,
“恩施”
,
“荆州”
,
“神农架”
,
“十堰”
,
“咸宁”
,
“襄阳”
,
“孝感”
,
“随州”
,
“黄石”
,
“荆门”
,
“鄂州”
“101200101”
,
“101200901”
,
“101200501”
,
“101201001”
,
“101200801”
,
“101201201”
,
“101201101”
,
“101200701”
,
“101200201”
,
“101200401”
,
“101201301”
,
“101200601”
,
“101201401”
,
“101200301”
     
湖南
    
“长沙”
,
“邵阳”
,
“常德”
,
“郴州”
,
“吉首”
,
“株洲”
,
“娄底”
,
“湘潭”
,
“益阳”
,
“永州”
,
“岳阳”
,
“衡阳”
,
“怀化”
,
“韶山”
,
“张家界”
    
“101250101”
,
“101250901”
,
“101250601”
,
“101250501”
,
“101251501”
,
“101250301”
,
“101250801”
,
“101250201”
,
“101250701”
,
“101251401”
,
“101251001”
,
“101250401”
,
“101251201”
,
“101250202”
,
“101251101”
     
浙江
    
“杭州”
,
“湖州”
,
“金华”
,
“宁波”
,
“丽水”
,
“绍兴”
,
“衢州”
,
“嘉兴”
,
“台州”
,
“舟山”
,
“温州”
    
“101210101”
,
“101210201”
,
“101210901”
,
“101210401”
,
“101210801”
,
“101210501”
,
“101211001”
,
“101210301”
,
“101210601”
,
“101211101”
,
“101210701”
     
江西
    
“南昌”
,
“萍乡”
,
“九江”
,
“上饶”
,
“抚州”
,
“吉安”
,
“鹰潭”
,
“宜春”
,
“新余”
,
“景德镇”
,
“赣州”
    
“101240101”
,
“101240901”
,
“101240201”
,
“101240301”
,
“101240401”
,
“101240601”
,
“101241101”
,
“101240501”
,
“101241001”
,
“101240801”
,
“101240701”
     
福建
    
“福州”
,
“厦门”
,
“龙岩”
,
“南平”
,
“宁德”
,
“莆田”
,
“泉州”
,
“三明”
,
“漳州”
    
“101230101”
,
“101230201”
,
“101230701”
,
“101230901”
,
“101230301”
,
“101230401”
,
“101230501”
,
“101230801”
,
“101230601”
     
贵州
    
“贵阳”
,
“安顺”
,
“赤水”
,
“遵义”
,
“铜仁”
,
“六盘水”
,
“毕节”
,
“凯里”
,
“都匀”
    
“101260101”
,
“101260301”
,
“101260208”
,
“101260201”
,
“101260601”
,
“101260801”
,
“101260701”
,
“101260501”
,
“101260401”
     
四川
    
“成都”
,
“泸州”
,
“内江”
,
“凉山”
,
“阿坝”
,
“巴中”
,
“广元”
,
“乐山”
,
“绵阳”
,
“德阳”
,
“攀枝花”
,
“雅安”
,
“宜宾”
,
“自贡”
,
“甘孜州”
,
“达州”
,
“资阳”
,
“广安”
,
“遂宁”
,
“眉山”
,
“南充”
    
“101270101”
,
“101271001”
,
“101271201”
,
“101271601”
,
“101271901”
,
“101270901”
,
“101272101”
,
“101271401”
,
“101270401”
,
“101272001”
,
“101270201”
,
“101271701”
,
“101271101”
,
“101270301”
,
“101271801”
,
“101270601”
,
“101271301”
,
“101270801”
,
“101270701”
,
“101271501”
,
“101270501”
     
广东
    
“广州”
,
“深圳”
,
“潮州”
,
“韶关”
,
“湛江”
,
“惠州”
,
“清远”
,
“东莞”
,
“江门”
,
“茂名”
,
“肇庆”
,
“汕尾”
,
“河源”
,
“揭阳”
,
“梅州”
,
“中山”
,
“德庆”
,
“阳江”
,
“云浮”
,
“珠海”
,
“汕头”
,
“佛山”
    
“101280101”
,
“101280601”
,
“101281501”
,
“101280201”
,
“101281001”
,
“101280301”
,
“101281301”
,
“101281601”
,
“101281101”
,
“101282001”
,
“101280901”
,
“101282101”
,
“101281201”
,
“101281901”
,
“101280401”
,
“101281701”
,
“101280905”
,
“101281801”
,
“101281401”
,
“101280701”
,
“101280501”
,
“101280800”
     
广西
    
“南宁”
,
“桂林”
,
“阳朔”
,
“柳州”
,
“梧州”
,
“玉林”
,
“桂平”
,
“贺州”
,
“钦州”
,
“贵港”
,
“防城港”
,
“百色”
,
“北海”
,
“河池”
,
“来宾”
,
“崇左”
    
“101300101”
,
“101300501”
,
“101300510”
,
“101300301”
,
“101300601”
,
“101300901”
,
“101300802”
,
“101300701”
,
“101301101”
,
“101300801”
,
“101301401”
,
“101301001”
,
“101301301”
,
“101301201”
,
“101300401”
,
“101300201”
     
云南
    
“昆明”
,
“保山”
,
“楚雄”
,
“德宏”
,
“红河”
,
“临沧”
,
“怒江”
,
“曲靖”
,
“思茅”
,
“文山”
,
“玉溪”
,
“昭通”
,
“丽江”
,
“大理”
    
“101290101”
,
“101290501”
,
“101290801”
,
“101291501”
,
“101290301”
,
“101291101”
,
“101291201”
,
“101290401”
,
“101290901”
,
“101290601”
,
“101290701”
,
“101291001”
,
“101291401”
,
“101290201”
     
海南
    
“海口”
,
“三亚”
,
“儋州”
,
“琼山”
,
“通什”
,
“文昌”
    
“101310101”
,
“101310201”
,
“101310205”
,
“101310102”
,
“101310222”
,
“101310212”
     
新疆
    
“乌鲁木齐”
,
“阿勒泰”
,
“阿克苏”
,
“昌吉”
,
“哈密”
,
“和田”
,
“喀什”
,
“克拉玛依”
,
“石河子”
,
“塔城”
,
“库尔勒”
,
“吐鲁番”
,
“伊宁”
    
“101130101”
,
“101131401”
,
“101130801”
,
“101130401”
,
“101131201”
,
“101131301”
,
“101130901”
,
“101130201”
,
“101130301”
,
“101131101”
,
“101130601”
,
“101130501”
,
“101131001”
     
西藏
    
“拉萨”
,
“阿里”
,
“昌都”
,
“那曲”
,
“日喀则”
,
“山南”
,
“林芝”
    
“101140101”
,
“101140701”
,
“101140501”
,
“101140601”
,
“101140201”
,
“101140301”
,
“101140401”
     
台湾
    
“台北”
,
“高雄”
    
“101340102”
,
“101340201”

天气返回的数据就是JSON格式,不需要自己提供数据,图片如下:

  iOS开发--JSON详解手机开发

 

4.JSON解析;

解析JSON一般有四种方式TouchJson、 SBJson 、JSONKit 和NSJSONSerialization,性能和速度的NSJSONSerialization>JSONKit>SBJSON>TouchJson,NSJSONSerialization是iOS5之后系统提供的,其他的为第三方类库,现在手机基本上都是iOS6以上,本文就从系统自带的NSJSONSerialization来解析JSON数据。

NSJSONSerialization有两个方法:

dataWithJSONObject:从OC对象转换成JSON数据

JSONObjectWithData:从JSON转成OC对象

1
2
3
4
5
6
7
8
/* Generate JSON data from a Foundation object. If the object will not produce valid JSON then an exception will be thrown. Setting the NSJSONWritingPrettyPrinted option will generate JSON with whitespace designed to make the output more readable. If that option is not set, the most compact possible JSON will be generated. If an error occurs, the error parameter will be set and the return value will be nil. The resulting data is a encoded in UTF-8.
 
*/
+ (
NSData 
*)dataWithJSONObject:(
id
)obj options:(
NSJSONWritingOptions
)opt error:(
NSError 
**)error;
 
/* Create a Foundation object from JSON data. Set the NSJSONReadingAllowFragments option if the parser should allow top-level objects that are not an NSArray or NSDictionary. Setting the NSJSONReadingMutableContainers option will make the parser generate mutable NSArrays and NSDictionaries. Setting the NSJSONReadingMutableLeaves option will make the parser generate mutable NSString objects. If an error occurs during the parse, then the error parameter will be set and the result will be nil.
   
The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.
 
*/
+ (
id
)JSONObjectWithData:(
NSData 
*)data options:(
NSJSONReadingOptions
)opt error:(
NSError 
**)error;

 

 

 

二.

在服务器上请求JSON数据到客户端

故事板中放置一个UITextView:

1
@property 
(weak, 
nonatomic

IBOutlet 
UITextView *myTextView;

viewDidLoad中代码: 

1
2
3
4
5
6
7
8
9
10
11
//设置URL
 
NSURL 
*url=[
NSURL 
URLWithString:@
http://m.weather.com.cn/data/101180601.html
];
 
//设置请求
 
NSURLRequest 
*request=[
NSURLRequest 
requestWithURL:url];
 
//接收数据
 
NSData 
*responseData=[
NSURLConnection 
sendSynchronousRequest:request returningResponse:
nil 
error:
nil
];
 
//解析数据
 
NSDictionary 
*weatherDic = [
NSJSONSerialization 
JSONObjectWithData:responseData options:
NSJSONReadingMutableLeaves 
error:
nil
];
 
//展示结果
 
NSDictionary 
*weatherResult= [weatherDic objectForKey:@
“weatherinfo”
];
self
.myTextView.text= [
NSString 
stringWithFormat:@
“今天是 %@  %@  %@  的天气状况:温度%@  %@  %@—FlyElephant:http://www.cnblogs.com/xiaofeixiang
,[weatherResult objectForKey:@
“date_y”
],[weatherResult objectForKey:@
“week”
],[weatherResult objectForKey:@
“city”
], [weatherResult objectForKey:@
“temp1”
],[weatherResult objectForKey:@
“weather1”
],[weatherResult objectForKey:@
“index_ls”
]];

跟之前的网络请求一样,代码中有解析,如果不明白可以参考之前文章,最终演示效果如下:

iOS开发--JSON详解手机开发

上传JSON数据到服务器

发送JSON数据给服务器

发送JSON数据给服务器的步骤:

(1)一定要使用POST请求

(2)设置请求头

(3)设置JSON数据为请求体

代码示例:

 1 #import "YYViewController.h" 
 2  
 3 @interface YYViewController () 
 4  
 5 @end 
 6  
 7 @implementation YYViewController 
 8  
 9 - (void)viewDidLoad 
10 { 
11     [super viewDidLoad]; 
12 } 
13  
14 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
15 { 
16     // 1.创建请求 
17     NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/order"]; 
18     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
19     request.HTTPMethod = @"POST"; 
20      
21     // 2.设置请求头 
22     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
23      
24     // 3.设置请求体 
25     NSDictionary *json = @{ 
26                            @"order_id" : @"123", 
27                            @"user_id" : @"789", 
28                            @"shop" : @"Toll" 
29                            }; 
30      
31 //    NSData --> NSDictionary 
32     // NSDictionary --> NSData 
33     NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil]; 
34     request.HTTPBody = data; 
35      
36     // 4.发送请求 
37     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
38         NSLog(@"%d", data.length); 
39     }]; 
40 } 
41  
42 @end

 

二、多值参数

多值参数:一个参数对应多个值。

如下面的请求参数:

http://192.168.1.103:8080/MJServer/weather?place=北京&place=河南&place=湖南

服务器的place属性是一个数组。因此用同一个参数不会把服务器的值覆盖。

注意:

我们用的AFN 第三方框架有部分 数据解析它已经帮我们做了;上传数据的时候也有部分接口:例如图片;

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/app/6080.html

(0)
上一篇 2021年7月17日 00:36
下一篇 2021年7月17日 00:36

相关推荐

发表回复

登录后才能评论