美文网首页
字典转字符串

字典转字符串

作者: 特特特 | 来源:发表于2016-07-16 17:09 被阅读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);

    }

    ```

    相关文章

      网友评论

          本文标题:字典转字符串

          本文链接:https://www.haomeiwen.com/subject/cvjujttx.html