现在的位置: 首页 > 综合 > 正文

创建JSON和解析JSON

2017年11月26日 ⁄ 综合 ⁄ 共 832字 ⁄ 字号 评论关闭

  //创建JSON
    NSDictionary *dictonary = [[NSMutableDictionary alloc] init];
    [dictonary setValue:@"MIle" forKey:@"name"];
    [dictonary setValue:@"19" forKey:@"age"];
    [dictonary setValue:@"male" forKey:@"sex"];
    NSDictionary *root = [[NSMutableDictionary alloc] init];
    [root setValue:dictonary forKey:@"player"];
    
    SBJsonWriter *writer = [[SBJsonWriter alloc] init];
    NSLog(@"Start Create JSON!");
    NSString *value = [writer stringWithObject:root];
    NSLog(value);
    
    //解析JSON
    NSDictionary *dicRoot = [[[SBJsonParser alloc]init] objectWithString:value];
    for (int i=0; i<[dicRoot count]; i++) {
        NSDictionary *player = [dicRoot objectForKey:@"player"];
        NSString *name = [player objectForKey:@"name"];
        NSString *age = [player objectForKey:@"age"];
        NSString *sex = [player objectForKey:@"sex"];
        NSLog(@"Name = %@", name);
        NSLog(@"Age = %@", age);
        NSLog(@"Sex = %@", sex);
        
    }

抱歉!评论已关闭.