iOS 关于转换json

2017-09-07  本文已影响5人  家振要翻天

工程里面会遇到一些关于字典包数组对象的情况 直接放进去是不行的 需要进行转换的方法

NSMutableDictionary*dictionary = [[NSMutableDictionaryalloc]init];

[dictionarysetValue:@"hello"forKey:@"one"];

[dictionarysetValue:@"world"forKey:@"two"];

NSArray*arry=[NSArrayarrayWithObjects:@"001",@"002",@"003",nil];

[dictionarysetValue:arryforKey:@"three"];

NSData*data=[NSJSONSerializationdataWithJSONObject:dictionaryoptions:NSJSONWritingPrettyPrintederror:nil];

NSString*jsonStr=[[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];

NSLog(@"jsonStr==%@",jsonStr);

输出结果为:

str=={

"one" : "hello",

"two" : "world",

"three" : [

"001",

"002",

"003"

]

}

上一篇下一篇

猜你喜欢

热点阅读