iOS 字符串转json 字符串转数组
//字符串转数组
- (id)toArrayOrNSDictionary:(NSString *)jsonData{
if (jsonData != nil) {
NSData* data = [jsonData dataUsingEncoding:NSUTF8StringEncoding];
id jsonObject = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingAllowFragmentserror:nil];
if (jsonObject != nil){
return jsonObject;
}else{
// 解析错误
return nil;
}
}
return nil;
}
//字符串转json
- (NSString *)toJSONData:(id)theData{
NSString * jsonString = @"";
if (theData != nil) {
NSData *jsonData = [NSJSONSerializationdataWithJSONObject:theData options:NSJSONWritingPrettyPrintederror:nil];
if ([jsonData length] != 0){
jsonString = [[NSStringalloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
}
return jsonString;
}
posted on 2021-04-02 14:27 yucaijiang 阅读(3251) 评论(0) 编辑 收藏 举报