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

 NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];

    [dictionary setValue:@"hello" forKey:@"one"];

    [dictionary setValue:@"world" forKey:@"two"];

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

    [dictionary setValue:arry forKey:@"three"];

    NSData *data=[NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];

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

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

输出结果为:

str=={

  "one" : "hello",

  "two" : "world",

  "three" : [

    "001",

    "002",

    "003"

  ]

}

 

原文地址:https://www.cnblogs.com/shenlaiyaoshi/p/7776638.html