1.将Object转换为JSONString :
- (NSString*)JSONString
{
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
if (error)
{
TTLog(@"StringjsonWithObject:Error:%@",error);
}
if (data)
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return nil;
}
2.将JSONString转换为Object:
- (id)JSONValue
{
NSData* data = [self dataUsingEncoding:NSUTF8StringEncoding];
NSError* error = nil;
id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONWritingPrettyPrinted error:&error];
if (error)
{
TTLog(@"JSONValue:Error:%@",error);
}
if (data)
return json;
return nil;
}