20150623_OC之Json格式的文件及URl解析
来源:互联网 发布:鲁班软件官网 编辑:程序博客网 时间:2024/06/08 13:46
//// main.m// IOS150623_ObjectiveC_Json文件解析//// Created by PengJunlong on 15/6/23.// Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>//json//javascript的子集//key:value 类似OC中的键值对//在文件中使用[]包含的是数组 ,使用{}包含的是字典对象,""包含的是字符串对象//1.json文件最外层结构通常为字典或者是数字,字典居多.//2.json数据可以为基本类型,字符串对象,数组对象([]包含),字典类型({}包含),布尔类型int main(int argc, const char * argv[]) { @autoreleasepool { //解析json时一般只使用NSJSONSerialization中的这一个方法:+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error; //1.先把文件的数据类型读成NSData类对象 NSString *jsonString = [NSString stringWithContentsOfFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_ObjectiveC_Json文件解析/IOS150623_ObjectiveC_Json文件解析/jsonUserList.txt" encoding:NSUTF8StringEncoding error:nil]; NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; /* Create a Foundation object from JSON data. Set the NSJSONReadingAllowFragments option if the parser should allow top-level objects that are not an NSArray or NSDictionary. Setting the NSJSONReadingMutableContainers option will make the parser generate mutable NSArrays and NSDictionaries. Setting the NSJSONReadingMutableLeaves option will make the parser generate mutable NSString objects. If an error occurs during the parse, then the error parameter will be set and the result will be nil. The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8. */ //+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error; // typedef NS_OPTIONS(NSUInteger, NSJSONReadingOptions) { // NSJSONReadingMutableContainers = (1UL << 0), // NSJSONReadingMutableLeaves = (1UL << 1), // NSJSONReadingAllowFragments = (1UL << 2) // } NS_ENUM_AVAILABLE(10_7, 5_0); //解析json文件时options一般都选择NSJSONReadingMutableContainers //2.解析json数据,文件最外层是字典对象 NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"dict = %@",dict); NSArray *userArray = [dict objectForKey:@"users"]; for (NSDictionary *dic in userArray) { NSLog(@"name = %@",[dic objectForKey:@"username"]); } } return 0;}
//// main.m// IOS150623_ObjectiveC_Json网址解析//// Created by PengJunlong on 15/6/23.// Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) { @autoreleasepool { //解析json网址时和解析json文件一样,只是数据的来源不同而已 NSURL *url = [NSURL URLWithString:@"http://gc.ditu.aliyun.com/regeocoding?l=39.9333,116.3959&type=001"]; NSString *urlString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; NSData *data = [urlString dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"dictionary = %@",dictionary); } return 0;}
0 0
- 20150623_OC之Json格式的文件及URl解析
- 20150625_OC之XML格式解析
- RTSP传输协议之URL格式及URL解析
- java解析json格式请求的url中参数
- 20150623_OC之属性列表文件plist
- 20150623_OC之文件归档NSKeyedArchiver
- [Javascript Practice] Url解析成Json格式
- 20150619_OC之NSData及文件操作NSFileHandle
- json 格式解析 及使用
- JSON格式的解析
- Android中JSON格式及JSON解析
- json解析之四种基本格式的解析
- JSON格式之GSON解析
- 20150612_OC之数组及复合类的练习
- 20150624_OC之代理及反向代理的简单例子
- JSON格式数据解析及GSON框架的使用详解
- iOS开发之JSON格式数据的生成与解析
- json的基本解析格式
- Deep Learning for Nature Language Processing---第四讲(上)
- HBase数据存储格式
- 上海会议总结
- 常用的JS插件介绍:4、swipe.js——手势幻灯片
- java也支持重载!
- 20150623_OC之Json格式的文件及URl解析
- 压测工具webbench源码分析
- c++ primer读书笔记-第七章 函数
- 动态规划之求序列里最长的非降序列
- Hibernate连接oracle数据库的简单例子
- LeetCode题解:Invert Binary Tree
- DB2常用命令总结
- 自定义不等高的cell
- 20150623_OC之文件归档NSKeyedArchiver