iOS data 转 json 串的实现

//data 转 json 字符串
- (NSString *)toJsonData:(id)theData{
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    if (jsonData.length > 0) {
        return jsonStr;
    }else{
        return nil;
    }
}

//UTF-8转码
[outLineStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

//UTF-8解码
[_outline stringByRemovingPercentEncoding];

你可能感兴趣的:(iOS data 转 json 串的实现)