如何转换 Swift NSDictionary 枚举钥匙字典吗
1个回答
2015-12-12
展开全部
+(NSString *)stringTOjson:(id)temps //把字典和数组转换成json字符串
{
NSData* jsonData =[NSJSONSerialization dataWithJSONObject:temps
options:NSJSONWritingPrettyPrinted error:nil];
NSString *strs=[[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
return strs;
}
利用NSJSONSerialization序列化成NSData,再通过NSString 转成JSON字符串
func toJSONString(dict:NSDictionary!)->NSString{
var data = NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions.PrettyPrinted, error: nil)
var strJson=NSString(data: data, encoding: NSUTF8StringEncoding)
return strJson
}
{
NSData* jsonData =[NSJSONSerialization dataWithJSONObject:temps
options:NSJSONWritingPrettyPrinted error:nil];
NSString *strs=[[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
return strs;
}
利用NSJSONSerialization序列化成NSData,再通过NSString 转成JSON字符串
func toJSONString(dict:NSDictionary!)->NSString{
var data = NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions.PrettyPrinted, error: nil)
var strJson=NSString(data: data, encoding: NSUTF8StringEncoding)
return strJson
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询