TouchJSON and SBJSON

来源:互联网 发布:淘宝客服回复快捷短语 编辑:程序博客网 时间:2024/05/21 14:55
TouchJSON___and_SBJSON.zip (137.6 KB)


- (void)viewDidLoad

{

    [superviewDidLoad];


    

   // 原始json字符串

   NSString *jsonStringSrc =@"{\"attributes\":{\"StreetName\":\"MASON\",\"StreetType\":\"ST\"},\"address\":\"1 MASON ST\",\"score\":\"75\"}";

   NSData *jsonData = [jsonStringSrcdataUsingEncoding:NSUTF8StringEncoding];

   NSDictionary *dictionary = [[CJSONDeserializerdeserializer] deserializeAsDictionary:jsonDataerror:nil];

    NSLog(@"parse: %@", dictionary);

    

   //构建json

   NSDictionary *attributesDict =[NSDictionarydictionaryWithObjectsAndKeys:@"MASON",@"StreetName", @"ST",@"StreetType", nil];

   NSDictionary *user1 =[NSDictionarydictionaryWithObjectsAndKeys:@"1 MASON ST",@"address", @"75",@"score", attributesDict, @"attributes",nil];


   NSDictionary *user2 =[NSDictionarydictionaryWithObjectsAndKeys:@"2 MASON ST",@"address", @"75",@"score", attributesDict, @"attributes",nil];

    

   NSDictionary *str1 = [NSDictionarydictionaryWithObjectsAndKeys:[NSArrayarrayWithObjects:user1, user2, nil],@"downLoadInfo",nil];

    

    jsonData = [[CJSONSerializerserializer] serializeObject:str1error:nil];

   NSString *jsonString2 = [[NSStringalloc] initWithData:jsonDataencoding:NSUTF8StringEncoding];

    

   //touch json序列化

   NSLog(@"\n1111111111------touch json: %@", jsonString2);

   //SBJson序列化

   NSLog(@"\n22222222222222------SBJson: %@", [str1JSONRepresentation]);

    

}





 //SBJSON解析

    {

        "listJsonString":"[

        

            {\"password\":\"111\",\"userName\":\"111\"},

            {\"password\":\"222\",\"userName\":\"222\"},

            {\"password\":\"333\",\"userName\":\"333\"}

        

        ]"

    

    }



- (void)requestFinished:(ASIHTTPRequest *)request {

NSString *tempJson = [requestresponseString];

NSLog(@"%@",tempJson);

NSDictionary *dic = [tempJsonJSONValue];

NSArray *arr = [[dicobjectForKey:@"listJsonString"]JSONValue];

for (int i =0; i < [arr count]; i++) {

NSDictionary *temp = [arrobjectAtIndex:i];

NSLog(@"名字:%@,密码:%@",[tempobjectForKey:@"userName"],[tempobjectForKey:@"password"]);

UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(10,70 + i * 60,200, 50)];

label.text = [NSStringstringWithFormat:@"名字:%@,密码:%@",[tempobjectForKey:@"userName"],[tempobjectForKey:@"password"]];

[self.viewaddSubview:label];

[labelautorelease];

}






原创粉丝点击