網(wǎng)絡(luò)數(shù)據(jù)解析之 JSON 解析

解析一下.json文檔

[
 {
 "name":"張三",
 "gender":"男",
 "age":"18"
 },
 {
 "name":"李四",
 "gender":"男",
 "age":"28"
 },
 {
 "name":"王六",
 "gender":"女",
 "age":"38"
 }
 ]
  • 聲明一個(gè)student 類.建立.h 和.m 文件
//student.h
#import <Foundation/Foundation.h>

@interface Student : NSObject
@property (nonatomic, retain) NSString *name,*age,*gender;
- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary;

@end
//student.m
#import "Student.h"

@implementation Student

- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:otherDictionary];//使用 kvc 對屬性賦值.
    }
    return self;
}


- (NSString *)description
{
    return [NSString stringWithFormat:@"name:%@,gender:%@,age:%@", self.name,self.gender,self.age];
}

@end
//viewController.h
#import "ViewController.h"
#import "Student.h"
@interface ViewController ()

@end

使用系統(tǒng)提供的類來進(jìn)行 json 解析

//使用系統(tǒng)提供的類進(jìn)行 json 數(shù)據(jù)解析
- (IBAction)parserJSON:(id)sender {
    //json文件的路徑,
   NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"];
    //提取內(nèi)容
    NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
    NSError *error = nil;
    
    NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    //判斷是否出現(xiàn)問題
    if (error == nil) {
        NSLog(@"%@",array);
    }else{
        NSLog(@"%@",error);
    }
    
    
    NSMutableArray  *studentArray = [[NSMutableArray alloc] initWithCapacity:1];
    
    for (NSDictionary *dic in array) {
        Student *student = [[Student alloc] initWithDictionary:dic];
        [studentArray addObject:student];
    }
    for (Student *stu in studentArray) {
        NSLog(@"%@",stu);
    }
}
//解析的結(jié)果為
2016-07-03 19:15:19.521 jsonParser[2147:180052] (
        {
        age = 18;
        gender = "\U7537";
        name = "\U5f20\U4e09";
    },
        {
        age = 28;
        gender = "\U7537";
        name = "\U674e\U56db";
    },
        {
        age = 38;
        gender = "\U5973";
        name = "\U738b\U516d";
    }
)
2016-07-03 19:15:19.522 jsonParser[2147:180052] name:張三,gender:男,age:18
2016-07-03 19:15:19.522 jsonParser[2147:180052] name:李四,gender:男,age:28
2016-07-03 19:15:19.522 jsonParser[2147:180052] name:王六,gender:女,age:38

使用第三方 JSONKit 來進(jìn)行解析,首先要下載有關(guān)的 JSONKit 的. h 和. m 文件
然后

//使用第三方 jsonkit 進(jìn)行 json數(shù)據(jù)解析.這樣做的好處就是可以清楚的觀察到解析的數(shù)據(jù).
- (IBAction)patserJSONKIT:(id)sender {
    //獲取json文件的路徑
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"];
    //獲取 json 文件的內(nèi)容
    NSString *contentString = [[NSString alloc] initWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];
    //使用 jsonkit進(jìn)行解析
    NSArray *array = [contentString objectFromJSONString];
    NSLog(@"%@",array);
    //數(shù)據(jù)封裝
    NSMutableArray *studentArray = [[NSMutableArray alloc] initWithCapacity:3];

    for (NSDictionary *dic in array) {
        Student *student = [[Student alloc] initWithDictionary:dic];
        [studentArray addObject:student];
    }
    for (Student *stu in studentArray) {
        NSLog(@"%@",stu);
    }
}
//解析的結(jié)果為
2016-07-03 19:17:45.691 jsonParser[2147:180052] (
        {
        age = 18;
        gender = "\U7537";
        name = "\U5f20\U4e09";
    },
        {
        age = 28;
        gender = "\U7537";
        name = "\U674e\U56db";
    },
        {
        age = 38;
        gender = "\U5973";
        name = "\U738b\U516d";
    }
)
2016-07-03 19:17:45.691 jsonParser[2147:180052] name:張三,gender:男,age:18
2016-07-03 19:17:45.691 jsonParser[2147:180052] name:李四,gender:男,age:28
2016-07-03 19:17:45.691 jsonParser[2147:180052] name:王六,gender:女,age:38
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末破镰,一起剝皮案震驚了整個(gè)濱河市蝙眶,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖勋眯,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件疲憋,死亡現(xiàn)場離奇詭異饭冬,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)篇亭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來锄贷,“玉大人译蒂,你說我怎么就攤上這事∫耆矗” “怎么了柔昼?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長因惭。 經(jīng)常有香客問我岳锁,道長,這世上最難降的妖魔是什么蹦魔? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任激率,我火速辦了婚禮,結(jié)果婚禮上勿决,老公的妹妹穿的比我還像新娘乒躺。我一直安慰自己,他們只是感情好低缩,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布嘉冒。 她就那樣靜靜地躺著,像睡著了一般咆繁。 火紅的嫁衣襯著肌膚如雪讳推。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天玩般,我揣著相機(jī)與錄音银觅,去河邊找鬼。 笑死坏为,一個(gè)胖子當(dāng)著我的面吹牛究驴,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播匀伏,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼洒忧,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了够颠?” 一聲冷哼從身側(cè)響起熙侍,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后蛉抓,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體牢硅,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年芝雪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了减余。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,680評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡惩系,死狀恐怖位岔,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情堡牡,我是刑警寧澤抒抬,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站晤柄,受9級(jí)特大地震影響擦剑,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜芥颈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一惠勒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧爬坑,春花似錦纠屋、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至署辉,卻和暖如春族铆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背哭尝。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工哥攘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人刚夺。 一個(gè)月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓献丑,卻偏偏與公主長得像末捣,于是被迫代替她去往敵國和親侠姑。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容