TouchJSON and SBJSON
TouchJSON___and_SBJSON.zip(137.6 KB)
- (void)viewDidLoad { [super viewDidLoad];
// 原始json字符串 NSString *jsonStringSrc = @"{"attributes":{"StreetName":"MASON","StreetType":"ST"},"address":"1 MASON ST","score":"75"}"; NSData *jsonData = [jsonStringSrc dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:nil]; NSLog(@"parse: %@",dictionary);
//构建json值 NSDictionary *attributesDict =[ NSDictionary dictionaryWithObjectsAndKeys:@"MASON",@"StreetName",@"ST",@"StreetType",nil]; NSDictionary *user1 =[NSDictionary dictionaryWithObjectsAndKeys:@"1 MASON ST",@"address",@"75",@"score",attributesDict,@"attributes",nil];
NSDictionary *user2 =[NSDictionary dictionaryWithObjectsAndKeys:@"2 MASON ST",nil];
NSDictionary *str1 = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:user1,user2,nil],@"downLoadInfo",nil];
jsonData = [[CJSONSerializer serializer] serializeObject:str1 error:nil]; NSString *jsonString2 = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//touch json序列化 NSLog(@"n2222222222------touch json: %@",jsonString2); //SBJson序列化 NSLog(@"n22222222222222------SBJson: %@",[str1 JSONRepresentation]);
}
//SBJSON解析
{ "listJsonString":"[
{"password":"111","userName":"111"}, {"password":"222","userName":"222"},14)"> {"password":"333","userName":"333"} ]"
}
- (void)requestFinished:(ASIHTTPRequest *)request {
NSString *tempJson = [request responseString]; NSLog(@"%@",tempJson); NSDictionary *dic = [tempJson JSONValue]; NSArray *arr = [[dic objectForKey:@"listJsonString"] JSONValue]; for (int i = 0; i < [arr count]; i++) {
NSDictionary *temp = [arr objectAtIndex:i]; NSLog(@"名字:%@,密码:%@",[temp objectForKey:@"userName"],[temp objectForKey:@"password"]); UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,70 + i * 60,200,50)]; label.text = [NSString stringWithFormat:@"名字:%@,密码:%@",[temp objectForKey:@"userName"],[temp objectForKey:@"password"]]; [self.view addSubview:label]; [label autorelease]; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |