博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS学习之路十九(JSON与Arrays 或者 Dictionaries相互转换)
阅读量:4705 次
发布时间:2019-06-10

本文共 2447 字,大约阅读时间需要 8 分钟。

今天写了个json与Arrays 或者 Dictionaries相互转换的例子很简单:

通过 NSJSONSerialization 这个类的 dataWithJSONObject: options: error:方法来实现。 

//dictionary序列化成json    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];    [dictionary setValue:@"Anthony"forKey:@"First Name"];    [dictionary setValue:@"Robbins"forKey:@"Last Name"];    [dictionary setValue:[NSNumber numberWithUnsignedInteger:51]forKey:@"Age"];    NSArray *arrayOfAnthonysChildren = [[NSArray alloc]                                        initWithObjects:                                        @"Anthony's Son 1", @"Anthony's Daughter 1", @"Anthony's Son 2", @"Anthony's Son 3", @"Anthony's Daughter 2",nil];    [dictionary setValue:arrayOfAnthonysChildren forKey:@"children"];    NSError *error = nil;    //序列化数据成json的data。。。。。。。。。。。。。。。。。。。。。。。    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary                                                       options:NSJSONWritingPrettyPrinted                                                         error:&error];    if ([jsonData length] > 0 && error == nil){        NSLog(@"已把字典成功序列化.");        //把json数据转化为String类型        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];        NSLog(@"JSON String = %@", jsonString);             //把 JSON 数据转化成 Arrays 或者 Dictionaries        //反序列化。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。        id jsonObject = [NSJSONSerialization                         JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments                         error:&error];        if (jsonObject != nil && error == nil){            NSLog(@"反序列化成功...");            if ([jsonObject isKindOfClass:[NSDictionary class]]){                NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;                NSLog(@"反序列化后的dictionary数据 = %@", deserializedDictionary);            }            else if ([jsonObject isKindOfClass:[NSArray class]]){                NSArray *deserializedArray = (NSArray *)jsonObject;                NSLog(@"反序列化json后的数组 = %@", deserializedArray);            }else {                            }                }else if (error != nil){            NSLog(@"反序列化时发生一个错误");        }            } else if ([jsonData length] == 0 && error == nil){       NSLog(@"序列化后没有返回数据");    }else if (error != nil){      NSLog(@"错误: %@", error);    }
转载请注明:

本文转自:

新浪微博:http://weibo.com/u/3202802157

转载于:https://www.cnblogs.com/lixingle/p/3312961.html

你可能感兴趣的文章
adb操作命令详解及大全
查看>>
javascript Browser 对象
查看>>
linux常用命令
查看>>
案例分析————博客园
查看>>
Fresco内存机制(Ashmem匿名共享内存)
查看>>
Python进行JSON格式化输出,以及汉字显示问题
查看>>
实验一词法分析试验报告
查看>>
关于移动应用的安全加固
查看>>
Gunicorn、Supervisor
查看>>
20150310+SVN版本控制-02
查看>>
Java:XML篇,使用DOM读取并解析XML
查看>>
elk系列3之通过json格式采集Nginx日志【转】
查看>>
SQL Server Management Studio(SSMS)的使用与配置整理
查看>>
【Python】Python发展历史
查看>>
递归再一次让哥震惊了
查看>>
fhq_treap || BZOJ 3224: Tyvj 1728 普通平衡树 || Luogu P3369 【模板】普通平衡树
查看>>
HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
查看>>
iOS-plist文件的写读
查看>>
例题:函数递归调用.有五个人按顺序排,第五个人比第四个人大2岁,以此类推,已知第一个人10岁,求第五个人年龄....
查看>>
django----ORM
查看>>