/**json
* 字典轉json數組
*/it
+ (NSString*)dictionaryToJson:(id)dicio
{table
NSError *parseError = nil;coding
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];error
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];dict
}tab
/**di
* jaon轉字典
*/
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
if (jsonString == nil) {
return nil;
}
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if(err) {
NSLog(@"json解析失敗:%@",err);
return nil;
}
return dic;
}
/******json轉數組******/
+ (NSArray *)arrayWithJsonString:(NSString *)jsonString{
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
return array;
}