Foundation对象(字典,数组等)转换为json串

  1. NSDictionary *song = [NSDictionary dictionaryWithObjectsAndKeys:@"i can fly",@"title",@"4012",@"length",@"Tom",@"Singer", nil];  
  2.     if ([NSJSONSerialization isValidJSONObject:song])  
  3.     {  
  4.         NSError *error;  
  5. ///将JSON数据写为NSData数据
  6.         NSData *jsonData = [NSJSONSerialization dataWithJSONObject:song options:NSJSONWritingPrettyPrinted error:&error];  
  7.         NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];  
  8.         NSLog(@"json data:%@",json);  
  9.     }  

猜你喜欢

转载自my.oschina.net/fllKevin/blog/1806084
今日推荐