首页 技术 正文
技术 2022年11月15日
0 收藏 624 点赞 2,275 浏览 2889 个字
////  ViewController.m//  Json////  Created by City--Online on 15/4/28.//  Copyright (c) 2015年 CYW. All rights reserved.//#define strUrl @"http://app.api.autohome.com.cn/autov3.2/news/newslist-a2-pm1-v3.2.0-c0-nt0-p1-s20-l0.html"#import "ViewController.h"#import "SBJson.h"#import "CJSONDeserializer.h"#import "CJSONSerializer.h"#import "JSONKit.h"@interface ViewController ()@property(nonatomic,strong)NSString *stringData;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];//    json解析    NSURL *url=[NSURL URLWithString:strUrl];    NSError *err=NULL;    //获取Json字符串    NSString *Jsonstr=[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&err];    NSData *data=[Jsonstr dataUsingEncoding:NSUTF8StringEncoding];//    第一种:IOS5以上自带的JSONObjectWithData//    typedef NS_OPTIONS(NSUInteger, NSJSONReadingOptions) {//        NSJSONReadingMutableContainers = (1UL << 0),//返回可变容器,NSMutableDictionary或NSMutableArray//        NSJSONReadingMutableLeaves = (1UL << 1),//返回的JSON对象中字符串的值为NSMutableString//        NSJSONReadingAllowFragments = (1UL << 2)//允许JSON字符串最外层既不是NSArray也不是NSDictionary,但必须是有效的JSON Fragment。例如使用这个选项可以解析 @“123” 这样的字符串。//    }    //解析    NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&err];    if (err==nil) {        NSLog(@"%@",[dic objectForKey:@"message"]);        NSArray *array=[[dic objectForKey:@"result"] objectForKey:@"focusimg"];        NSLog(@"%@",[array objectAtIndex:1]);    }    //生成    //判断是否能转为Json    if ([NSJSONSerialization isValidJSONObject:dic]) {        //转为Json        NSData *data= [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];        NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];        NSLog(@"%@",str);        //json再次转为字典         NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&err];        NSLog(@"%@",dic);    }//    第二种:SBJson//      解析    //引入#import "SBJson.h"    SBJsonParser *parser=[[SBJsonParser alloc]init];    NSDictionary *SBJsonDic=[parser objectWithString:Jsonstr error:nil];    NSLog(@"%@",SBJsonDic);    SBJsonWriter  *sbwriter=[[SBJsonWriter alloc]init];    NSString *sbstr=[sbwriter stringWithObject:SBJsonDic];    NSLog(@"%@",sbstr);//    第三种:Touch Json//    解析//    #import "TouchJson/JSON/CJSONDeserializer.h"    NSDictionary *touchDic=[[CJSONDeserializer deserializer] deserialize:data error:nil];    NSLog(@"%@",touchDic);//    生成    NSString *touchstr=[[NSString alloc]initWithData: [[CJSONSerializer serializer] serializeDictionary:touchDic error:nil] encoding:NSUTF8StringEncoding];    NSLog(@"%@",touchstr);//    第四种:JsonKit//    使用Jsonkit时引入第三方稍微麻烦//    第一步:引入Jsonkit//    第二步:JsonKit支持MRC,不支持ARC(伟哥指导纠正thanks)。点击Targets->Build Phases->Compile Sources中找到JsonKit.m点击输入-fno-objc-arc//    第三步:在Build Setting中Levels中搜索 Direct usage of 'isa'设置为NO    //生成    NSDictionary *JsonKitDic=[Jsonstr objectFromJSONString];    NSLog(@"%@",JsonKitDic);    //解析    NSString *JsonKitstr=[JsonKitDic JSONString];    NSLog(@"%@",JsonKitstr);}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

Json比较简单,直接上代码,具体说明找度娘,JsonKit在ios9中会出现闪退报错 需要下载最新的JsonKit

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295