码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 网络浅析-(七 JSON解析之三方JSONKit)

时间:2016-03-18 21:42:36      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

在这个网络横行的时代.........

有没有小说的感觉,哈哈??。

言归正传,之前我写过XML的网络解析,但是现在的app开发很少有用到XML解析的了,主流的则是JSON。(有时间我会对其进行总结)三方更是让json解析更加简便,由于代码过于简单,请大家做好心理准备。不要被吓到哦。

//
//  ViewController.m
//  CX-JSON解析(三方JSONKit-master)
//
//  Created by ma c on 16/3/18.
//  Copyright © 2016年 xubaoaichiyu. All rights reserved.
//

#import "ViewController.h"
#import "JSONKit.h"
@interface ViewController ()


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString * urlString = @"http://localhost/city.json";
    
    NSURL * url = [NSURL URLWithString:urlString];
    
    NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url cachePolicy:0 timeoutInterval:15];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
       //重点是这行代码,理解就可以,这个三方很好用。
        NSDictionary * dict = [data objectFromJSONData];
        
        NSArray * array = [dict objectForKey:@"city"];
        
        for (NSDictionary * dictTwo in array) {
            
            NSLog(@"%@",dictTwo);
            
        }
        
        
    }];
    
}
/*
 一部分结果
 2016-03-18 19:53:11.441 CX-JSON解析(三方JSONKit-master)[6130:389488] {
 city =     (
 {
 "city_id" = 1;
 "city_name" = "\U897f\U57ce\U533a";
 },
 {
 "city_id" = 2;
 "city_name" = "\U671d\U9633\U533a";
 },
 {
 "city_id" = 3;
 "city_name" = "\U6d77\U6dc0\U533a";
 },
 {
 "city_id" = 4;
 "city_name" = "\U4e30\U53f0\U533a";
 },
 {
 "city_id" = 5;
 "city_name" = "\U77f3\U666f\U5c71\U533a";
 },
 {
 "city_id" = 6;
 "city_name" = "\U901a\U5dde\U533a";
 },
 {
 "city_id" = 7;
 "city_name" = "\U987a\U4e49\U533a";
 },
 {
 "city_id" = 8;
 "city_name" = "\U623f\U5c71\U533a";
 },
 {
 "city_id" = 9;
 "city_name" = "\U5927\U5174\U533a";
 },
 {
 "city_id" = 10;
 "city_name" = "\U660c\U5e73\U533a";
 },
 {
 "city_id" = 11;
 "city_name" = "\U6000\U67d4\U533a";
 },
 {
 "city_id" = 12;
 "city_name" = "\U5e73\U8c37\U533a";
 },
 {
 "city_id" = 13;
 "city_name" = "\U95e8\U5934\U6c9f\U533a";
 },
 {
 "city_id" = 14;
 "city_name" = "\U5bc6\U4e91\U53bf";
 },
 
   */

@end

 

IOS 网络浅析-(七 JSON解析之三方JSONKit)

标签:

原文地址:http://www.cnblogs.com/xubaoaichiyu/p/5293400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!