当前位置: 首页 > news >正文

推进门户网站建设网络营销的四个策略

推进门户网站建设,网络营销的四个策略,3g开发网站,网站程序是什么?NSJSONSerialization介绍: NSJSONSerialization提供了将JSON数据转换为Foundation对象(一般都是NSDictionary和NSArray)和Foundation对象转换为JSON数据(可以通过调用isValidJSONObject来判断Foundation对象是否可以转换为JSON数据…

NSJSONSerialization介绍:

NSJSONSerialization提供了将JSON数据转换为Foundation对象(一般都是NSDictionaryNSArray)和Foundation对象转换为JSON数据(可以通过调用isValidJSONObject来判断Foundation对象是否可以转换为JSON数据)

转换成JSON的对象必须具有如下属性

1.顶层对象必须是NSArray或者NSDictionary

2.所有的对象必须是NSString、NSNumber、NSArray、NSDictionary、NSNull

3.所有NSDictionary的key必须是NSString类型

4.数字对象不能是非数值或无穷(NSNumber)

相关参数介绍
1. NSJSONReadingOptions

NSJSONReadingMutableContainers 创建可变的数组或字典 接收 

NSJSONReadingMutableLeaves   指定在JSON对象可变字符串被创建为NSMutableString的实例 

NSJSONReadingAllowFragments   指定解析器应该允许不属于的NSArrayNSDictionary中的实例顶层对象

2. NSJSONWritingOptions

NSJSONWritingPrettyPrinted的意思是将生成的json数据格式化输出,这样可读性高,不设置则输出的json字符串就是一整行。

3.NSDictionary中的key就是json字符串中的key,object就是json字符串中的value,
转化问题:(json<---->Foundation)

1. json对象转换为Foundation数据(+(id)JSONObjectWithData: options: error:)(其中NSData不能为空)

   if (data) {

                NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

                NSLog(@"%@",dict);

            }

2.Foundation对象转换为json数据(+(NSData *)dataWithJSONObject: options:error:;(其中对象不能为空)

 NSDictionary *dict = @{@"1" : @"a",@"2" : @"b"};

//    NSArray *arr = @[@"1",@"2"];
     if ([NSJSONSerialization isValidJSONObject:dict]) {
        NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
         NSString *json = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
         NSLog(@"%@",json);
    }

发送json数据到服务器:1.是post请求    2.设置请求头   3.设置json数据为请求体

2.[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

3.NSData *requestData = [NSJSONSerialization dataWithJSONObject:dictData options:NSJSONWritingPrettyPrinted error:nil];

JSON转化的函数意思:

+ (BOOL)isValidJSONObject:(id)obj;//方法是检测Foundation对象能否合法转换为JSON对象

+ (nullable NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error;//给一个数组或字典对象转化成NSData对象

+ (nullable id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;//给一个NSData对象,转化成数组或字典

+ (NSInteger)writeJSONObject:(id)obj toStream:(NSOutputStream *)stream options:(NSJSONWritingOptions)opt error:(NSError **)error;//将JSON数据写入到输出流,返回的是写入流的字节数

+ (nullable id)JSONObjectWithStream:(NSInputStream *)stream options:(NSJSONReadingOptions)opt error:(NSError **)error;//从输入流读取JSON数据




===================

json和xml的普及个人觉得是为了简化阅读难度,以及减轻网络负荷,json和xml 数据格式在格式化以后都是一种树状结构,可以树藤摸瓜的得到你想要的任何果子。

而不格式化的时候json和xml 又是一个普普通通的字符串,在网络通信的时候也只需要请求一次,而不用每次为得到木一个值而重复的请求服务器或者目标主机,

json和xml 都采用 键 - 值 的形式来存放数据。

xml 使用: <键> 值 </键>

json 使用:  "键" : "值"

苹果公司提供了一个官方的json解析库 NSJSONSerialization    

NSJSONSerialization  里面包含了两个方法来通过不同数据形式来解析json数据。

1、+ ( id )JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError ** )error; //使用缓冲区数据来解析 

2、+ ( id )JSONObjectWithStream:(NSInputStream *)stream options:(NSJSONReadingOptions)opt error:(NSError **)error; // 使用文件流的形式来解析json

/* 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;/* Create a JSON object from JSON data stream. The stream should be opened and configured. All other behavior of this method is the same as the JSONObjectWithData:options:error: method.*/
+ (id)JSONObjectWithStream:(NSInputStream *)stream options:(NSJSONReadingOptions)opt error:(NSError **)error;

解析json的步骤大概是,先把json字符串读取到缓冲区,然后使用NSJSONSerialization     里面的方法进行解析,根据不同json 格式可能返回的数据类型不一样,所以最好用 id 类型接。

eg:  id dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];

在得到解析后的数据时,然后就一步一步 ,一个一个 键 - 值 的去寻找你想要的咚咚。

Eg 1 : 从本地文件中读取json数据,然后读取到缓冲区。

- (void)jsonParse{//初始化文件路径。NSString* path  = [[NSBundle mainBundle] pathForResource:@"nanjing" ofType:@"txt"];//将文件内容读取到字符串中,注意编码NSUTF8StringEncoding 防止乱码,NSString* jsonString = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];//将字符串写到缓冲区。NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];//解析json数据,使用系统方法 JSONObjectWithData:  options: error:NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];//接下来一步一步解析。知道得到你想要的东西。NSArray* arrayResult =[dic objectForKey:@"results"]; NSDictionary* resultDic = [arrayResult objectAtIndex:0];NSDictionary* geometryDic = [resultDic objectForKey:@"geometry"];NSLog(@"geometryDic: %@,  resultDic:%@",geometryDic,resultDic);NSDictionary* locationDic = [geometryDic objectForKey:@"location"];NSNumber* lat = [locationDic objectForKey:@"lat"];NSNumber* lng = [locationDic objectForKey:@"lng"];NSLog(@"lat = %@, lng = %@",lat,lng);[jsonString release];}

Eg 2 :使用网络路径来解析json,

- (void)jsonParse{//初始化网络路径。NSString* path  = @"http://maps.googleapis.com/maps/api/geocode/json?address=nanjing&sensor=true";//初始化 url NSURL* url = [NSURL URLWithString:path];//将文件内容读取到字符串中,注意编码NSUTF8StringEncoding 防止乱码,NSString* jsonString = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];//将字符串写到缓冲区。NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];//解析json数据,使用系统方法 JSONObjectWithData:  options: error:NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];//一下为自定义解析, 自己想怎么干就怎么干NSArray* arrayResult =[dic objectForKey:@"results"]; NSDictionary* resultDic = [arrayResult objectAtIndex:0];NSDictionary* geometryDic = [resultDic objectForKey:@"geometry"];NSLog(@"geometryDic: %@,  resultDic:%@",geometryDic,resultDic);NSDictionary* locationDic = [geometryDic objectForKey:@"location"];NSNumber* lat = [locationDic objectForKey:@"lat"];NSNumber* lng = [locationDic objectForKey:@"lng"];NSLog(@"lat = %@, lng = %@",lat,lng);[jsonString release];}

Eg 3 :使用网络路径来解析json 。 使用NSURLRequest 和NSURLConnection 请求网络数据。

- (void)jsonParse{//初始化网络路径。NSString* path  = @"http://maps.googleapis.com/maps/api/geocode/json?address=nanjing&sensor=true";//初始化 url NSURL* url = [NSURL URLWithString:path];NSURLRequest* request = [NSURLRequest requestWithURL:url];//将请求到的字符串写到缓冲区。NSData* jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];//解析json数据,使用系统方法 JSONObjectWithData:  options: error:NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];//一下为自定义解析, 自己想怎么干就怎么干NSArray* arrayResult =[dic objectForKey:@"results"]; NSDictionary* resultDic = [arrayResult objectAtIndex:0];NSDictionary* geometryDic = [resultDic objectForKey:@"geometry"];NSLog(@"geometryDic: %@,  resultDic:%@",geometryDic,resultDic);NSDictionary* locationDic = [geometryDic objectForKey:@"location"];NSNumber* lat = [locationDic objectForKey:@"lat"];NSNumber* lng = [locationDic objectForKey:@"lng"];NSLog(@"lat = %@, lng = %@",lat,lng);}

http://www.lbrq.cn/news/2366515.html

相关文章:

  • 保定免费做网站seo常用工具有哪些
  • 广东网站建设找哪家网站推广方法大全
  • 什么是企业网站建设福州seo顾问
  • wordpress繁体版济南网站优化公司哪家好
  • 福建网站开发招聘潍坊网站开发公司
  • 长春网站关键词排名关键词优化的技巧
  • 网站建设技术网站建设北京seo课程
  • 网站开发招标书中国新闻今日头条
  • 邛崃做网站广告的六种广告形式
  • 最好的网页设计网站百度热线
  • 安阳网站建设推广优化自己建网站
  • 天猫商城的商品来源上海抖音seo
  • 南通网站建设知识餐饮营销案例100例
  • python官网下载安装黄山seo排名优化技术
  • 免费做网站怎么做网站619总裁班课程培训
  • 简述网站建设的标准智能建站网站模板
  • 网站备案 关闭今天的国内新闻
  • 门户网站建设重要性注册网站域名
  • 网站制作的趋势搜索引擎案例分析结论
  • 沈阳市 建委 网站普通话的顺口溜6句
  • 南海网官网seo工作是什么意思
  • 怎么做网站推广的步骤搜索引擎优化期末考试答案
  • 做简单的动态网站教程网站建站哪家公司好
  • 如何建立网站站点超链接友情外链查询
  • 河南网站建设公司 政府北京seo专业团队
  • 山东聊城建设局网站智能建站系统
  • 淘宝网站建设哪个类目seo优化外包
  • 做网站的过程网络推广员的前景
  • 惠州做网站开发网络营销渠道名词解释
  • 天津怎么建立企业网站seo搜索引擎优化论文
  • 【人工智能99问】梯度消失、梯度爆炸的定义、后果及规避手段?(7/99)
  • centos中新增硬盘挂载文件夹
  • 界面控件Kendo UI for Angular 2025 Q2新版亮点 - 增强跨设备的无缝体验
  • 网络安全初级(Python实现sql自动化布尔盲注)
  • 智能电网时代:双向WiFi电表在海外家庭能源中的战略价值
  • 项目流程管理系统使用建议:推荐13款