zoukankan      html  css  js  c++  java
  • 网络热恋之json解析

    现在的app开发很少有用到XML解析的了,主流的则是JSON。

      1 //
      2 //  ViewController.m
      3 //  CX-JSON解析(三方JSONKit-master)
      4 
      5 #import "ViewController.h"
      6 #import "JSONKit.h"
      7 @interface ViewController ()
      8 
      9 
     10 @end
     11 
     12 @implementation ViewController
     13 
     14 - (void)viewDidLoad {
     15     [super viewDidLoad];
     16 
     17     NSString * urlString = @"http://localhost/city.json";
     18     
     19     NSURL * url = [NSURL URLWithString:urlString];
     20     
     21     NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url cachePolicy:0 timeoutInterval:15];
     22     
     23     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
     24        //重点是这行代码,理解就可以,这个三方很好用。
     25         NSDictionary * dict = [data objectFromJSONData];
     26         
     27         NSArray * array = [dict objectForKey:@"city"];
     28         
     29         for (NSDictionary * dictTwo in array) {
     30             
     31             NSLog(@"%@",dictTwo);
     32             
     33         }
     34         
     35         
     36     }];
     37     
     38 }
     39 /*
     40  一部分结果
     41  2016-03-18 19:53:11.441 CX-JSON解析(三方JSONKit-master)[6130:389488] {
     42  city =     (
     43  {
     44  "city_id" = 1;
     45  "city_name" = "U897fU57ceU533a";
     46  },
     47  {
     48  "city_id" = 2;
     49  "city_name" = "U671dU9633U533a";
     50  },
     51  {
     52  "city_id" = 3;
     53  "city_name" = "U6d77U6dc0U533a";
     54  },
     55  {
     56  "city_id" = 4;
     57  "city_name" = "U4e30U53f0U533a";
     58  },
     59  {
     60  "city_id" = 5;
     61  "city_name" = "U77f3U666fU5c71U533a";
     62  },
     63  {
     64  "city_id" = 6;
     65  "city_name" = "U901aU5ddeU533a";
     66  },
     67  {
     68  "city_id" = 7;
     69  "city_name" = "U987aU4e49U533a";
     70  },
     71  {
     72  "city_id" = 8;
     73  "city_name" = "U623fU5c71U533a";
     74  },
     75  {
     76  "city_id" = 9;
     77  "city_name" = "U5927U5174U533a";
     78  },
     79  {
     80  "city_id" = 10;
     81  "city_name" = "U660cU5e73U533a";
     82  },
     83  {
     84  "city_id" = 11;
     85  "city_name" = "U6000U67d4U533a";
     86  },
     87  {
     88  "city_id" = 12;
     89  "city_name" = "U5e73U8c37U533a";
     90  },
     91  {
     92  "city_id" = 13;
     93  "city_name" = "U95e8U5934U6c9fU533a";
     94  },
     95  {
     96  "city_id" = 14;
     97  "city_name" = "U5bc6U4e91U53bf";
     98  },
     99  
    100    */
    101 
    102 @end
  • 相关阅读:
    ios UIWebView截获html并修改便签内容(转载)
    IOS获取系统时间 NSDate
    ios 把毫秒值转换成日期 NSDate
    iOS  如何判断当前网络连接状态  网络是否正常  网络是否可用
    IOS开发 xcode报错之has been modified since the precompiled header was built
    iOS系统下 的手机屏幕尺寸 分辨率 及系统版本 总结
    iOS 切图使用 分辨率 使用 相关总结
    整合最优雅SSM框架:SpringMVC + Spring + MyBatis 基础
    Java面试之PO,VO,TO,QO,BO
    Notes模板说明
  • 原文地址:https://www.cnblogs.com/iOSlearner/p/5381388.html
Copyright © 2011-2022 走看看