iOS对象(字典或数组)转化为JSon字符串

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"

]

}

你可能感兴趣的:(iOS对象(字典或数组)转化为JSon字符串)