ios json數據解析

轉自:

http://blog.csdn.net/chonbj/article/details/25982511


剛剛下午那會 弄了個 解析 xml  demo的小例子,本想着json也挺複雜 弄還是 不弄,但是簡單的看了下 發現挺簡單,這個時候就猶豫了,要不寫到博客上來 ?

考慮了很久,還是寫上來吧,畢竟json用得太多了,而且算是自己的積累吧,畢竟剛開始學習IOS開發方面的知識,就當是鞏固了撒!

還是 先看個效果圖吧,如下!



接下來 看下工程目錄吧,其實並沒有必要,直接建立一個工程就行 ,算了,還是貼上來吧,如下:


工程目錄中有個 Notes.json 文件,該文件就是 要解析的json數據了 ,也截下圖吧,如下:



Ok ,以上準備完畢,就開始編碼了,在此之前故事版的內容 就和我上篇博客文章 IOS 解析xml 故事版 是一樣配置的,這裏就不在囉嗦了 ,首先看下 chonViewController.h文件,代碼如下:

[objc] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. //  
  2. //  chonViewController.h  
  3. //  TestJson  
  4. //  
  5. //  Created by choni on 14-5-16.  
  6. //  Copyright (c) 2014年 choni. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface chonViewController : UITableViewController  
  12.   
  13. //保存數據列表  
  14. @property(nonatomic,strongNSMutableArray * listData;   
  15.   
  16. @end  

與之對應的 chonViewController.m文件 代碼如下:

[objc] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. //  
  2. //  chonViewController.m  
  3. //  TestJson  
  4. //  
  5. //  Created by choni on 14-5-16.  
  6. //  Copyright (c) 2014年 choni. All rights reserved.  
  7. //  
  8.   
  9. #import "chonViewController.h"  
  10.   
  11. @interface chonViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation chonViewController  
  16.   
  17. - (void)viewDidLoad  
  18. {  
  19.     [super viewDidLoad];  
  20.       
  21.       
  22.     NSString * path = [[NSBundle mainBundle]pathForResource:@"Notes" ofType:@"json" ];  
  23.     NSData * jsonData = [[NSData alloc] initWithContentsOfFile:path];  
  24.       
  25.     NSError * error ;  
  26.     id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];  
  27.     if (!jsonObj || error) {  
  28.         NSLog(@"JSON解析失敗");  
  29.     }  
  30.     self.listData = [jsonObj objectForKey:@"Record"];  
  31.       
  32.       
  33. }  
  34. #pragma mark - tableView  
  35. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
  36.     return 1;  
  37. }  
  38.   
  39. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  40.     return self.listData.count;  
  41. }  
  42.   
  43. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  44.   
  45.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];  
  46.       
  47.     NSMutableDictionary * dict = self.listData[indexPath.row];  
  48.     cell.textLabel.text = [dict objectForKey:@"Content"];  
  49.     cell.detailTextLabel.text = [dict objectForKey:@"CDate"];  
  50.     return cell ;  
  51. }  
  52.   
  53.   
  54.   
  55. - (void)didReceiveMemoryWarning  
  56. {  
  57.     [super didReceiveMemoryWarning];  
  58. }  
  59.   
  60. @end  

Ok , 現在就可以編譯運行的程序了 ,但是有個主意的地方  :

1.因爲使用 NSJSONSerialization 實現json解碼,要確定你得項目使用IOS 5 SDK 纔可以

2. 其他的就沒有什麼了,介紹下NSJSONSerialization的類方法吧

   1)NSJSONReadingMutableContaines ,指定解析返回的是可變的數組或字典 ,這個方法還是比較使用的,因爲如果json數據需要改,不用管撒

   2)NSJSONReadingMutableLeaves ,指定葉節點是可變的字符串

   3)   NSJSONReadingAllowFragments , 指定頂級節點可以部署數組或字典


 另,json是輕量級的數據交換格式。。。。什麼的,去你大爺的,但在應用開發的時候 優選考慮json 而不是xml  ,xml 最先考慮的應當是TBXML,上篇博客提到過,json 最先

考慮NSJSONSerialization 。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章