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

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

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

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

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

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

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

NSString*jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

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

输出结果为:

str=={

    "one" : "hello",

    "two" : "world",

    "three" : [

                "001",

                "002",

                "003"

              ]

    }

 


版权声明:本文为shanghaibao123原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。