将JSON转化为NSArray或者NSDictionary
2015-12-12 10:30
543 查看
1 获得JSON文件的路径(以project.json文件为例)
NSString *path=[[NSBundle
mainBundle]pathForResource:@"project"
ofType:@"json"];
2 加载JSON文件
NSData *data=[NSData
dataWithContentsOfFile:path];
3 将JSON转化为NSArray或者NSDictionary
NSDictionary *dict
[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray *Array=[NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:nil];
此处options 选项有三:
NSJSONReadingMutableContainers
Specifies that arrays and dictionaries are created as mutable objects. // 创建可变的数组或字典 接收
NSJSONReadingMutableLeaves
Specifies that leaf strings in the JSON object graph are created as instances of NSMutableString. // 指定在JSON对象可变字符串被创建为
NSMutableString的实例
NSJSONReadingAllowFragments
Specifies that the parser should allow top-level objects that are not an instance of NSArray or NSDictionary // 指定解析器应该允许不属于的NSArray或NSDictionary中的实例顶层对象
NSString *path=[[NSBundle
mainBundle]pathForResource:@"project"
ofType:@"json"];
2 加载JSON文件
NSData *data=[NSData
dataWithContentsOfFile:path];
3 将JSON转化为NSArray或者NSDictionary
NSDictionary *dict
[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray *Array=[NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:nil];
此处options 选项有三:
NSJSONReadingMutableContainers
Specifies that arrays and dictionaries are created as mutable objects. // 创建可变的数组或字典 接收
NSJSONReadingMutableLeaves
Specifies that leaf strings in the JSON object graph are created as instances of NSMutableString. // 指定在JSON对象可变字符串被创建为
NSMutableString的实例
NSJSONReadingAllowFragments
Specifies that the parser should allow top-level objects that are not an instance of NSArray or NSDictionary // 指定解析器应该允许不属于的NSArray或NSDictionary中的实例顶层对象
相关文章推荐
- JavaScript学习(5.7.3):"use strict"
- JavaScript学习(5.7.2):debugger 语句
- js 定时跳转, 格式化字符串时间
- 关于json返回日期格式化的解决方案
- 10013---JavaScript--正则表达式
- JavaScript匿名函数之模仿块级作用域
- JSON使用JsonPropertyAttribute
- JavaScript 概述
- Json转换
- Json转换
- lazyload.js学习摘记
- doT.js 模板引擎学习摘记
- JSTL标签 封装jqGrid表格插件
- JS-Demo2:JavaScript版TableGrid,表格头、分页表格冻结,表格头可拉动
- JS-Demo1:JavaScript实现表格列拖动
- JavaScript解析过程你真的清楚吗??
- js----Number对象
- JS Map 和 List 的简单实现代码
- 10012---JavaScript--类型转换
- js----显示当前系统时间