//数组转json
- (NSString *)arrayToJson:(NSArray *)array
{
NSString *json=nil;
NSError *error =nil;
NSData *data= [NSJSONSerializationdataWithJSONObject:array options:0 error:&error];
if (!error) {
json = [[NSString alloc]initWithData:dataencoding:NSUTF8StringEncoding];
return json;
}else
{
return error.localizedDescription;
}
}
//字典转json
- (NSString *)dictionaryToJson:(NSDictionary *)dic
{
NSString *json=nil;
NSError *error =nil;
NSData *data= [NSJSONSerializationdataWithJSONObject:dic options:NSJSONWritingPRettyPrintederror:&error];
if (!data) {
return @"{}";
}else if (!error) {
json = [[NSString alloc]initWithData:dataencoding:NSUTF8StringEncoding];
return json;
}else
{
return error.localizedDescription;
}
}
新闻热点
疑难解答