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(@"\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];

}

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...