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", @"address", @"75", @"score", attributesDict, @"attributes", 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\"},

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

}






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值