-(NSDictionary*)Login:(NSString *)email password:(NSString *)password
{
NSMutableDictionary *resultsDictionary;// 返回的 JSON 数据
NSDictionary *userDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:password, @"password",email,@"email",nil];
if ([NSJSONSerialization isValidJSONObject:userDictionary])
{
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userDictionary options:NSJSONWritingPrettyPrinted error: &error];
NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
NSURL *url = [NSURL URLWithString:@"http://seanchense.com/login"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"];
[request addRequestHeader:@"Accept" value:@"application/json"];
[request setRequestMethod:@"POST"];
[request setPostBody:tempJsonData];
[request startSynchronous];
NSError *error1 = [request error];
if (!error1)
{
NSString *response = [request responseString];
NSLog(@"Test:%@",response);
NSData* jsonData = [response dataUsingEncoding:NSUTF8StringEncoding];
}
}
return resultsDictionary;
}
//实例化一个书本管理类
Book_manager *bm = [Book_manager new];
//读入文件
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//NSLog(@"%@",content); //这句话可以用来测试是否读取到数据
//转换成二进制数据
NSData * data = [content dataUsingEncoding:NSUTF8StringEncoding];
//解析JSON文件 OC中自带的方法
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//过滤节点,如果是{}就是词典,如果是[]就是数组,层层过滤
dict = [dict objectForKey:@"root"];
dict = [dict objectForKey:@"books"];
NSArray * arr = [dict objectForKey:@"book"];
//遍历数组并添加进书本管理
for (NSDictionary * dd in arr) {
//实例化一本书
BOOK *book = [BOOK new];
book.Id = [dd objectForKey:@"-id"];
book.language = [dd objectForKey:@"-language"];
book.name = [dd objectForKey:@"name"];
book.price = [[dd objectForKey:@"price"] doubleValue];
book.auther_name = [[dd objectForKey:@"auther"] objectForKey:@"name"];
book.summary = [dd objectForKey:@"summary"];
//添加书本进管理类中
[bm addBOOK:book];
}
参考:https://segmentfault.com/a/1190000000519598,https://www.cnblogs.com/dengsir/articles/5162216.html