- (void)viewDidLoad
{
[super viewDidLoad];
// 原始json字符串
NSString *jsonStringSrc = @"{\"attributes\":{\"StreetName\":\"MASON\",\"StreetType\":\"ST\"},\"address\":\"1 MASON ST\",\"score\":\"75\"}";
NSData *jsonData = [jsonStringSrc dataUsingEncoding:NSUTF8StringEncoding];
@H_301_65@ 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];
@H_301_65@ jsonData = [[CJSONSerializer serializer] serializeObject:str1 error:nil];
NSString *jsonString2 = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//touch json序列化
NSLog(@"\n1111111111------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];
}