json字符串转字典、字典转json字符串

json字符串转字典

+(id)toArrayOrNSDictionary:(NSString*)NOjsonString

{

if(NOjsonString.length==0) {

returnnil;

}

NSError*error =nil;

NSData*StatusData = [NOjsonString dataUsingEncoding:NSUTF8StringEncoding]; 

//NSLog(@"%@",StatusData);

idjsonObject = [NSJSONSerialization JSONObjectWithData:StatusData

options:NSJSONReadingAllowFragments

error:&error];

if(jsonObject !=nil&& error ==nil){

returnjsonObject;

}else{

//解析错误

returnnil;

}

}


字典转json字符串

+(NSString*)dictionaryToJson:(NSDictionary*)dic

{

NSError*parseError =nil;

NSData*jsonData = [NSJSONSerializationdataWithJSONObject:dicoptions:NSJSONWritingPrettyPrintederror:&parseError];

return[[NSStringalloc]initWithData:jsonDataencoding:NSUTF8StringEncoding];

}

你可能感兴趣的:(json字符串转字典、字典转json字符串)