- (NSString *)jsonStringFromObject:(id)object { //OC对象转JSON
NSError *erro = nil;
NSData *jsondata = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:&erro];
NSString *jsonString = [[NSString alloc]initWithData:jsondata encoding:NSUTF8StringEncoding];
return jsonString;
}
//把jsonString转换成任意对象
- (id)jsonStringToObject:(NSString *)jsonString
{
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
id object = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
if(error) {
NSLog(@"json解析失败:%@",error);
return nil;
}
return object;
}
网友评论