美文网首页
iOS:data,dic之间的转化

iOS:data,dic之间的转化

作者: money_ac9e | 来源:发表于2018-09-03 17:45 被阅读0次

dic->data

   NSMutableDictionary *muDic = [NSMutableDictionary dictionary];
    [muDic setValue:@(89.6) forKey:@"number"];

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:muDic options:kNilOptions error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

data -> dic

   NSData *jsonData2 = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    id dic = [NSJSONSerialization JSONObjectWithData:jsonData2 options:NSJSONReadingMutableContainers error:&err];
    if(err) {
        NSLog(@"json解析失败:%@",err);
    }else{
        NSLog(@"dic is %@",dic);
    }

相关文章

网友评论

      本文标题:iOS:data,dic之间的转化

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