日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

IOS开发之JSON序列化从客户端发送到服务器端

發布時間:2023/12/18 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS开发之JSON序列化从客户端发送到服务器端 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

IOS開發之JSON序列化從客戶端發送到服務端的準備工作

共有6種情況 需要序列化
請查看源代碼。 服務器端接受我們采用的是java的Tomcat服務器。配合 struts 2 controller框架

// // ViewController.m // 19-jsonxuliehua // // Created by 魯軍 on 2021/2/13. //#import "ViewController.h"#import "HMVideo.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{NSString *jsonStr = @"{\"name\":\"dajun\",\"age\":@(18)}";//[self postJson:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]];//字典NSDictionary *dict = @{@"order_id":@"123",@"user_id":@"124",@"shop":@"Toll"};//json 序列化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";//KVC 給對象內部的成員變量賦值[v1 setValue:@(YES) forKey:@"_isYellow"];if(![NSJSONSerialization isValidJSONObject:v1]){NSLog(@"無法進行JSon序列化操作");return;}// 把自定義的對象轉換成字典。 KVCNSDictionary *dict2 =[v1 dictionaryWithValuesForKeys:@[@"videoName",@"size",@"author",@"_isYellow"]];NSData *data3=[NSJSONSerialization dataWithJSONObject:dict2 options:0 error:NULL];[self postJson:data3];//NSLog(@"%@",v1);NSData *data2 = [NSJSONSerialization dataWithJSONObject:v1 options:0 error:NULL];// NSLog(@"%@",[[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding]);[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 // // HMVideo.h // 19-jsonxuliehua // // Created by 魯軍 on 2021/2/13. //#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 // // HMVideo.m // 19-jsonxuliehua // // Created by 魯軍 on 2021/2/13. //#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序列化从客户端发送到服务器端的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。