字典转字符串
2016-07-16 本文已影响37人
特特特
//两个字典--》数组 --> JSON --> 打印
```objc
NSDictionary *person = @{@"name":@"张三", @"age":@20, @"sex":@(YES), @"skills": @[@"Objective-C", @"Swift"]};
NSDictionary *secondPerson = @{@"name":@"李四", @"age":@25, @"sex":@(NO), @"skills": @[@"Ruby", @"Java"]};
NSArray *personArray = @[person, secondPerson];
/**
[{},{}]
{}
*/
//判定是否可以转
BOOL isJosnObj = [NSJSONSerialization isValidJSONObject:personArray];
if (isJosnObj) {
//personArray -> NSData(JSONData) -> NString
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:personArray options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"json字符串:%@", jsonStr);
}
```