//联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄
在和服务器交互过程中,会iOS字典或者数组和JSON串的转换,具体互换如下:
//将字典或者数组转化为JSON串
+ (NSData *)toJSONData:(id)theData
{
NSError*error =nil;
NSData*jsonData =[NSJSONSerialization dataWithJSONObject:theData options:NSJSONWritingPrettyPrinted error:nil];if([jsonData length]&&error==nil){returnjsonData;
}else{returnnil;
}
}
//将JSON串转化为字典或者数组
+ (id)toArrayOrNSDictionary:(NSData *)jsonData{
NSError*error =nil;idjsonObject =[NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingAllowFragments
error:nil];if(jsonObject != nil && error ==nil){returnjsonObject;
}else{//解析错误returnnil;
}
}
谢谢!!!