JSON解析---初识
JSON(JavaScript Object Notation)
是一种轻量级的数据交换格式
全然独立于语言的文本格式
易于人阅读和编写
易于解析和生成
(网络传输速度快)
JSON语法规则
数据在
名称/值
对中
数据有逗号分隔
花括号保存对象
方括号保存数组
{
"class1":[
{
"name":"zhangsan",
"age":"14",
"tel":"123123123"
},
{
"name":"lisi",
"age":"13",
"tel":"123123123"
},
{
"name":"wangwu",
"age":"12",
"tel":"123123123"
}
],
"class2":[
{
"name":"zhaoliu",
"age":"15",
"tel":"123123123"
},
{
"name":"wuwu",
"age":"16",
"tel":"123123123"
},
{
"name":"liuliu",
"age":"17",
"tel":"123123123"
}
]
}
ViewController.m文件
// JSON解析
#import "ViewController.h"
#import "JSONKit.h"
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
NSString * path= [[NSBundle
mainBundle] pathForResource:@"test.json"
ofType:nil];
NSData * data=[NSData
dataWithContentsOfFile:path];
// NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
/*
//NSLog(@"%@",dic); //打印整个JSON文件
*/
/*
NSArray * array=dic[@"class1"]; //拆一层
NSLog(@"%@",array);
*/
/*
NSArray * array=dic[@"class1"];
for(NSDictionary * dic in array)
{
NSLog(@"%@",dic); //拆两层
}
*/
//得到model
/*
NSArray * array=dic[@"class1"]; //拆一层
//NSLog(@"%@",array);
for( NSDictionary * dic in array)
{
NSData * data=[NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
NSString * str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}
*/
//打印第一组的人员姓名
/*
NSArray * array=dic[@"class1"]; //拆一层
//NSLog(@"%@",array);
NSMutableArray * marray=[[NSMutableArray alloc]init];
for( NSDictionary * dic in array)
{
[marray addObject:dic[@"name"]];
}
NSLog(@"%@",marray);
*/
//引入JSONKit
//打印整个JSON文件
/*
NSDictionary * dic=[data objectFromJSONData];
NSLog(@"%@",dic);
*/
/*
NSString * str=@"{\"name\":\"zhangsan\"}";
NSData * datas=[str dataUsingEncoding:NSUTF8StringEncoding];
id obj=[datas objectFromJSONData];
NSLog(@"%@",obj);
*/
}
@end
PS:推荐一个站点 www.bejson.com
能够将没有组织结构的.JSON
文件上传。就能够轻松的转变成可视化的视图结构。
JSON解析---初识的更多相关文章
- Android okHttp网络请求之Json解析
前言: 前面两篇文章介绍了基于okHttp的post.get请求,以及文件的上传下载,今天主要介绍一下如何和Json解析一起使用?如何才能提高开发效率? okHttp相关文章地址: Android o ...
- Json解析工具的选择
前言 前段时间@寒江不钓同学针对国内Top500和Google Play Top200 Android应用做了全面的分析(具体分析报告见文末的参考资料),其中有涉及到对主流应用使用json框架Gson ...
- iOS json 解析遇到error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 38 ...
- Android之JSON解析
做个Android网络编程的同学一定对于JSON解析一点都不陌生,因为现在我们通过手机向服务器请求资源,服务器给我们返回的数据资源一般都是以JSON格式返回,当然还有一些通过XML格式返回,相对JSO ...
- Android总结之json解析(FastJson Gson 对比)
前言: 最近为了统一项目中使用的框架,发现项目中用到了两种json解析框架,他们就是当今非常主流的json解析框架:google的Gson 和阿里巴巴的FastJson,为了废除其中一个所以来个性能和 ...
- JSON解析和XML解析对比
JSON解析和XML解析是较为普遍的两种解析方式,其中JSON解析的市场分额更大.本文系统的分析两种解析方式的区别,为更好地处理数据作准备.由于目前阶段主要是做移动开发,所以本文所描述的JSON解析和 ...
- iOS json解析的几种方法 NSJSONSerialization,JSONKit,SBJson ,TouchJson
相关的第三方类库大家可以去github上下载 1.NSJSONSerialization 具体代码如下 : - (void)viewDidLoad { [super viewDidLoad]; NSD ...
- Json解析工具Jackson(使用注解)
原文http://blog.csdn.net/nomousewch/article/details/8955796 接上一篇文章Json解析工具Jackson(简单应用),jackson在实际应用中给 ...
- Json解析工具Jackson(简单应用)
原文http://blog.csdn.net/nomousewch/article/details/8955796 概述 Jackson库(http://jackson.codehaus.org),是 ...
随机推荐
- 使用memcached加速web应用实例
在实际应用中,一般会把数据库查询的结果保存到memcached中,下次訪问数据库时直接从memcached中获取.而不再进行数据库操作,这样非常大的程度上减轻了数据库的负担. [演示样例]: < ...
- SUSE 在Intel举行"Rule The Stack"的竞赛中获得 "Openstack安装最高速"奖
有关"Rule The Stack": https://communities.intel.com/community/itpeernetwork/datastack/blog/2 ...
- 关于在xp(sp3 专业版)下安装sql2005开发版图解
今天我在xp上安装sql2005,搞了一上午也没有搞好,最终自己还是搞好,也装了,也卸载了!这里就总结一下,让以后用sql2005的朋友能有个参考!我也是自己在GOOGLE上搜索的! 转自:http: ...
- UML基础知识
UML:Unified Modeling Language,即统一建模语言.是一种图形化的建模语言标准. 如上图,UML可以帮助我们做软件需求分析和软件设计两方面的工作,在不同的应用场景中,UML的一 ...
- arcgis server manager - An error has occured on the server. For details please check the Event (Application) log on the web.
当登陆 Arcgis Server Manager的时候,点击 "Services" 中的选项"Manage Services",就报错: An error h ...
- 后台的Activity被系统回收怎么办?
onSaveIntanceState,当程序中的某个Activity A在运行中,主动或者被动的运行另外一个新的Activity B,这个时候 A就会执行onSaveIntanceState(Bund ...
- noip2015运输计划
二分+LCA+查分前缀和 #include<iostream> #include<cstring> #include<cstdio> #include<alg ...
- 利用.htaccess绑定域名到子目录
前提首先得把域名绑定绑定了,比如把dev.ccvita.com解析到211.136.108.190这个IP其次是在网站管理面板里,为网站绑定dev.ccvita.com最后编辑配置.htaccess文 ...
- IE,火狐,谷歌浏览器下js判断滚动条是否已拉到页面最底部
E/FF/Chrome下document.documentElement和document.body的 scrollHeight/scrollTop/clientHeight 以及判断滚动条是否已拉到 ...
- win7系统64位"禁用驱动程序签名强制"如何设置?
换了Win7系统64位旗舰版的朋友是不是都发现了一点,以前32位Win7系统用的好好的小程序小应用,一下子就装不上了.这是由于Win7系统64位系统对于未有认证签名的驱动程序进行了限制安装,而大部分小 ...