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

2016-08-05  本文已影响0人  我是菜鸟66666

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];

}

上一篇下一篇

猜你喜欢

热点阅读