iOS Json的解析与创建

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
+ (NSString *)voiceMessageConentWithEntity:(VMessageEntity *)entity{

    NSLog(@"entity.voiceMessage.voicemd5 == %@",entity.voiceMessage.voicemd5);

    if (!entity) {

        return nil;

    }

    VChatVoiceEntity *voiceMessage = entity.voiceMessage;

    if (!voiceMessage) {

        return nil;

    }

    NSString *voicemd5 = voiceMessage.voicemd5;

    NSString *duration = voiceMessage.duration;

    if (entity.messageType == VMessageTypeVoice) {

        NSMutableDictionary *rootDict = [NSMutableDictionary dictionary];

        if (voicemd5) {

            [rootDict setObject:voicemd5 forKey:@"md5"];

        }

        if (duration) {

            [rootDict setObject:duration forKey:@"duration"];

        }

       NSError *error = nil;

        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:rootDict options:NSJSONWritingPrettyPrinted error:&error];

        if (error) {

            NSLog(@"%@", error);

        }

        NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

        return json;

    }else{

        return nil;

    }


}


+ (VChatVoiceEntity *)voiceWithJson:(NSString *)json{

    if (!json.length) {

        return nil;

    }

    NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];

    NSError *error = nil;

    NSDictionary *root = [NSJSONSerialization JSONObjectWithData:jsonData

                                                         options:NSJSONReadingMutableLeaves

                                                           error:&error];

    if (root) {

        VChatVoiceEntity *voiceMessage = [[VChatVoiceEntity alloc] init];

        voiceMessage.voicemd5 = [root objectForKey:@"md5"];

        voiceMessage.duration = [root objectForKey:@"duration"];

        return voiceMessage;

    }

    return nil;


}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:python中使用MD5加密字符串

下一篇:python通过MD5文件校验来查找重复内容的文件