//
//  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

网络之Json生成解析的更多相关文章

  1. 一个简单的 JSON 生成/解析库

    这是一个单文件的,适用于C语言的, JSON 读写库. 先说明,不想造轮子,代码是从这里拿来的: https://www.codeproject.com/Articles/887604/jWrite- ...

  2. C#使用LitJson对Json数据解析

    JSON 介绍 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - ...

  3. Android网络请求与数据解析,使用Gson和GsonFormat解析复杂Json数据

    版权声明:未经博主允许不得转载 一:简介 [达叔有道]软件技术人员,时代作者,从 Android 到全栈之路,我相信你也可以!阅读他的文章,会上瘾!You and me, we are family ...

  4. Win(Phone)10开发第(3)弹,简单的Demo程序网络请求json解析列表显示

    先分享一个由Json字符串直接生成解析对应的类的工具: jsonclassgenerator14 百度天气接口 下面是由一个小功能(又特么的是天气)的实现,记录下下UAP的流程和结构(其实跟之前一模一 ...

  5. iOS网络编程解析协议三:JSON数据传输解析

    作为一种轻量级的数据交换格式,正在逐步取代XML,成为网络数据的通用格式 基于JavaScript的一个子集 易读性略差,编码手写难度大,数据量小 JSON格式取代了XML给网络传输带来了很大的便利, ...

  6. Qt之JSON生成与解析

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - December ...

  7. iOS开发网络篇-JSON文件的解析

    一.什么是JSON数据 1.JSON的简单介绍 JSON:是一种轻量级的传输数据的格式,用于数据的交互 JSON是javascript语言的一个子集.javascript是个脚本语言(不需要编译),用 ...

  8. iOS开发网络篇—JSON数据的解析

    iOS开发网络篇—JSON数据的解析 iOS开发网络篇—JSON介绍 一.什么是JSON JSON是一种轻量级的数据格式,一般用于数据交互 服务器返回给客户端的数据,一般都是JSON格式或者XML格式 ...

  9. Android Json生成及解析实例

    JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...

随机推荐

  1. android延时弹出软键盘

    searchEditView.setFocusable(true); searchEditView.setFocusableInTouchMode(true); searchEditView.requ ...

  2. shell 命令 -- 漂亮的资源查看命令 htop

    htop 相较top,htop更加直接和美观.

  3. mysql 唯一索引UNIQUE使用方法详解

    创建唯一索引的目的不是为了提高访问速度,而只是为了避免数据出现重复.唯一索引可以有多个但索引列的值必须唯一,索引列的值允许有空值.如果能确定某个数据列将只包含彼此各不相同的值,在为这个数据列创建索引的 ...

  4. 提示 make: 没有什么可以做的为 `all'

    提示 make: 没有什么可以做的为 `all'. make clean 一次,编译过程又有了.

  5. SoftWareHelper

    SoftWareHelper 环境 Visual Studio 2017,.Net Framework 4.0 SDK GitHub源码:https://github.com/yanjinhuagoo ...

  6. 背水一战 Windows 10 (50) - 控件(集合类): ItemsControl - 基础知识, 数据绑定, ItemsPresenter, GridViewItemPresenter, ListViewItemPresenter

    [源码下载] 背水一战 Windows 10 (50) - 控件(集合类): ItemsControl - 基础知识, 数据绑定, ItemsPresenter, GridViewItemPresen ...

  7. 【计算机网络】网络层学习笔记:总结IP,NAT和DHCP

    前言:这篇文章是学习网络层协议时候总结的笔记,前面的主要部分介绍的都是IP协议, 后半部分介绍NAT协议和DHCP协议 参考书籍 <计算机网络-自顶向下>       作者 James F ...

  8. pyqt5 Button.click 报错:argument 1 has unexpected type 'NoneType'

    如上所示,在括号中,添加‘lambda:’,就可以成功运行,不知道为啥. 参考:https://blog.csdn.net/flhsxyz/article/details/79220936?utm_s ...

  9. [ZJOI2010]基站选址(线段树优化dp)

    坑待填. \(Code\ Below:\) #include <bits/stdc++.h> #define lson (rt<<1) #define rson (rt< ...

  10. 程序猿的日常——Mybatis现学现卖

    最近有一个小项目需求,需要用spring mvc + mybatis实现一个复杂的配置系统.其中遇到了很多不太常见的问题,在这里特意记录下: 主要涉及的内容有 事务 多表删除 插入并返回主键 1 sp ...