NSMutableDictionary转化为JSON字符串数据样例

时间:2015-10-23 19:01:45   收藏:0   阅读:2914
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
        [dict setObject:@"1" forKey:@"one"];
        dict[@"dc"]=@{@"two":@"2"};
        NSLog(@"dict:%@",dict);
        
        NSData *data=[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
       NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"JsonStr:%@",str);
    }
    return 0;
}
/打印结果为:dict:{
    dc =     {
        two = 2;
    };
    one = 1;
}
2015-10-23 16:43:25.025 Test110[6562:614592] JsonStr:{
  "one" : "1",
  "dc" : {
    "two" : "2"
  }
}


原文:http://my.oschina.net/u/2410306/blog/521280

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!