iOS JSONKit的一些常用方法記錄

來源:互聯網
上載者:User

在項目中與伺服器互動的時候,經常要遇到解析json的情況,如果有同學想要解析JSON,那麼JSONKit可以是一個不錯的選擇


首先可以去gitHub上下載JSONKit 地址:JSONKit


放入工程以後,把JSONKit設定為不支援arc的模式



然後在點m修改2個地方<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">//array->isa = _JKArrayClass; object_setClass(array, _JKArrayClass);// dictionary->isa = _JKDictionaryClass; object_setClass(dictionary, _JKDictionaryClass);原因是isa已經被廢棄了,所以請調用object_setClass()和object_getClass()函數來搞定

這裡特別設定了2個json串,供大家參考學習

arr.conf

[{"preview_sub": "adornment_fly_1_100.png", "width": "300", "path": "adornment_fly_1.png", "name": "adornment_fly_1.png", "height": "300"}, {"preview_sub": "adornment_fly_2_100.png", "width": "300", "path": "adornment_fly_2.png", "name": "adornment_fly_2.png", "height": "300"}]

dict.conf

{"status": "ok", "after": "14851", "before": "11011"}

NSString *res = nil;        //數組轉json串    NSArray *arr = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",nil];    res = [arr JSONString];    NSLog(@"res= %@", [NSString stringWithString: res]);        //字典類型(對象)轉json串    NSArray *arr1 = [NSArray arrayWithObjects:@"dog",@"cat",nil];    NSArray *arr2 = [NSArray arrayWithObjects:[NSNumber numberWithBool:YES],[NSNumber numberWithInt:30],nil];    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:arr1,@"pets",arr2,@"other",nil];    res = [dic JSONString];    NSLog(@"res= %@", [NSString stringWithString: res]);        //json串轉數組    NSString* arrConf = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"arr" ofType:@"conf"] encoding:NSUTF8StringEncoding error:nil];    NSArray *ConfArr = [arrConf objectFromJSONString];    NSLog(@"%@",ConfArr);        //json串轉字典    NSString* dicConf = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dict" ofType:@"conf"] encoding:NSUTF8StringEncoding error:nil];    NSArray *ConfDic = [dicConf objectFromJSONString];    NSLog(@"%@",ConfDic);


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.