生活随笔
收集整理的這篇文章主要介紹了
IOS开发之JSON序列化从客户端发送到服务器端
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS開發之JSON序列化從客戶端發送到服務端的準備工作
共有6種情況 需要序列化
請查看源代碼。 服務器端接受我們采用的是java的Tomcat服務器。配合 struts 2 controller框架
#import "ViewController.h"#import "HMVideo.h"@interface ViewController
()@end@implementation ViewController
- (void)viewDidLoad
{[super viewDidLoad
];}- (void)touchesBegan
:(NSSet
<UITouch
*> *)touches withEvent
:(UIEvent
*)event
{NSString
*jsonStr
= @"{\"name\":\"dajun\",\"age\":@(18)}";NSDictionary
*dict
= @{@"order_id":@"123",@"user_id":@"124",@"shop":@"Toll"};NSData
*data1
= [NSJSONSerialization dataWithJSONObject
:dict options
:0 error
:NULL];[self postJson
:data1
];NSArray
*array
= @[@{@"name":@"zahngsan",@"age":@(18)},@{@"name":@"lisi",@"age":@(20)}];NSData
*data
= [NSJSONSerialization JSONObjectWithData
:array options
:0 error
:NULL];[self postJson
:data
];HMVideo
*v1
=[[HMVideo alloc
] init
];v1
.videoName
= @"demo.avi";v1
.size
= 100;v1
.author
= @"dajun";[v1 setValue
:@(YES
) forKey
:@"_isYellow"];if(![NSJSONSerialization isValidJSONObject
:v1
]){NSLog(@"無法進行JSon序列化操作");return;}NSDictionary
*dict2
=[v1 dictionaryWithValuesForKeys
:@[@"videoName",@"size",@"author",@"_isYellow"]];NSData
*data3
=[NSJSONSerialization dataWithJSONObject
:dict2 options
:0 error
:NULL];[self postJson
:data3
];NSData
*data2
= [NSJSONSerialization dataWithJSONObject
:v1 options
:0 error
:NULL];[self postJson
:data2
];HMVideo
*v2
=[[HMVideo alloc
] init
];v2
.videoName
= @"demo.avi";v2
.size
= 200;v2
.author
= @"韓梅梅";NSArray
*arr2
= @[v1
,v2
];NSMutableArray
*mArray
=[[NSMutableArray alloc
] initWithCapacity
:arr2
.count
];for (HMVideo
*hmv
in arr2
) {NSDictionary
*dict
=[hmv dictionaryWithValuesForKeys
:@[@"videoName",@"size",@"author",@"_isYellow"]];[mArray addObject
:dict
];}NSData
*data5
=[NSJSONSerialization dataWithJSONObject
:mArray options
:0 error
:NULL];[self postJson
:data5
];}-(void)postJson
:(NSData
*) data
{NSURL
*url
= [NSURL URLWithString
:@"http://localhost:8080/MJServer/order"];NSMutableURLRequest
*request
= [NSMutableURLRequest requestWithURL
:url
];request
.HTTPMethod
=@"post";request
.HTTPBody
=data
;[NSURLConnection sendAsynchronousRequest
:request queue
:[NSOperationQueue mainQueue
] completionHandler
:^(NSURLResponse
* _Nullable response
, NSData
* _Nullable data
, NSError
* _Nullable connectionError
) {if(connectionError
){NSLog(@"連接錯誤 %@",connectionError
);return;}NSHTTPURLResponse
*httpResponse
= (NSHTTPURLResponse
*)response
;if(httpResponse
.statusCode
==200||httpResponse
.statusCode
==304){NSString
*str
= [[NSString alloc
] initWithData
:data encoding
:NSUTF8StringEncoding
];NSLog(@"%@",str
);}else{NSLog(@"服務器內部錯誤");}}];}@end
#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN
@interface HMVideo
: NSObject
@property(nonatomic
,copy
)NSString
*videoName
;
@property(nonatomic
,copy
)NSString
*author
;
@property(nonatomic
,assign
)int size
;@endNS_ASSUME_NONNULL_END
#import "HMVideo.h"@implementation HMVideo
{BOOL _isYellow
;}- (NSString
*)description
{return [NSString stringWithFormat
:@"{%@{videoName:%@,size:%d,author=%@,_isYellow=%d}}", [super description
],self.videoName
,self.size
,self.author
,_isYellow
];
}
@end
總結
以上是生活随笔為你收集整理的IOS开发之JSON序列化从客户端发送到服务器端的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。