JSON序列化
来源:互联网 发布:京东618和双11数据 编辑:程序博客网 时间:2024/06/07 16:00
//
// ViewController.m
// 09-JSON序列化
//
// Created by Apple on 15/10/23.
// Copyright © 2015年 heima. All rights reserved.
//
#import "ViewController.h"
#import "HMVideo.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//1 自己拼json形式的字符串,比较困难
// NSString *jsonStr = @"{\"name\":\"zhangsan\",\"age\":18}";
// [self postJSON:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]];
//2 字典
// NSDictionary *dic = @{@"name":@"zhangsan",@"age":@(18)};
// //JSON序列化
// NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:0 error:NULL];
// [self postJSON:data];
//3 数组
// NSArray *array = @[
// @{@"name":@"zhangsan",@"age":@(18)},
// @{@"name":@"lisi",@"age":@(19)}
// ];
// //JSON序列化
// NSData *data = [NSJSONSerialization dataWithJSONObject:array options:0 error:NULL];
// [self postJSON:data];
//4 自定义对象进行JSON序列化
// HMVideo *v1 = [[HMVideo alloc] init];
// v1.videoName = @"ll-001.avi";
// v1.size = 500;
// v1.author = @"lilei";
// //KVC给对象内部的成员变量赋值
// [v1 setValue:@(NO) forKey:@"_isYellow"];
//
//
//// NSLog(@"%@",v1);
// //自定义对象不能进行JSON序列化,必须先把自定义对象转换成字典或数组
//// if (![NSJSONSerialization isValidJSONObject:v1]) {
//// NSLog(@"sorry,对象不能进行json序列化");
//// return;
//// }
//
// //把自定义对象转换成字典 KVC
// NSDictionary *dic = [v1 dictionaryWithValuesForKeys:@[@"videoName",@"size",@"author",@"_isYellow"]];
//
// NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:0 error:NULL];
// [self postJSON:data];
//5 把自定义对象的数组进行JSON序列化
HMVideo *v1 = [[HMVideo alloc] init];
v1.videoName = @"ll-001.avi";
v1.size = 500;
v1.author = @"lilei";
//KVC给对象内部的成员变量赋值
[v1 setValue:@(YES) forKey:@"_isYellow"];
HMVideo *v2 = [[HMVideo alloc] init];
v2.videoName = @"hmm-001.avi";
v2.size = 500;
v2.author = @"韩梅梅";
//KVC给对象内部的成员变量赋值
[v2 setValue:@(NO) forKey:@"_isYellow"];
NSArray *array = @[v1,v2];
// if (![NSJSONSerialization isValidJSONObject:array]) {
// NSLog(@"sorry,不能进行JSON序列化");
// return;
// }
//把自定义对象的数组,所有的对象都转换成字典
NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:2];
for (HMVideo *video in array) {
NSDictionary *dic = [video dictionaryWithValuesForKeys:@[@"videoName",@"size",@"author",@"_isYellow"]];
[mArray addObject:dic];
}
NSData *data = [NSJSONSerialization dataWithJSONObject:mArray options:0 error:NULL];
[self postJSON:data];
}
//
- (void)postJSON:(NSData *)data {
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/php/upload/postjson.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//设置post
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
- 序列化和json
- Json序列化
- JSON序列化类
- 序列化与json
- json的序列化
- C# Json序列化
- 序列化方法json
- IOS JSON 序列化
- Ext json序列化
- C#序列化JSON
- json 格式序列化
- json序列化
- vs2003 序列化json
- Json序列化
- Json序列化
- DataTable序列化Json
- 序列化和json
- Json 序列化
- Android一种高效压缩图片的方法
- linux字符驱动之自动创建设备节点
- ListView 图片加载+内存缓存+图片压缩
- JSONObject与JSONArray的使用
- seajs
- JSON序列化
- mysql之锁
- php-fpm的安装和启动
- Android 三大图片缓存原理、特性对比
- Ubnutu14.4+WN722N无线网卡做热点
- 2016年过年感觉
- Git命令
- linux 分区
- linux字符驱动之点亮LED