日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

【OC】JSONModel基本使用

發布時間:2023/12/14 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【OC】JSONModel基本使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JSONModel是一個庫,讓我們能快速創建智能數據模型。

基本使用

假設我有這樣一個JSON數據

{"country":"Germany","dialCode": 49,"isInEurope":YES }
  • 創建一個新的Objective-C類,并使其繼承JSONModel類。
  • 聲明與JSON鍵同名的屬性。
  • #import "JSONModel.h"NS_ASSUME_NONNULL_BEGIN@interface CountryModel : JSONModel@property (nonatomic, copy) NSString* country; @property (nonatomic, copy) NSString* dialCode; @property (nonatomic, assign) BOOL isInEurope;@endNS_ASSUME_NONNULL_END
  • 初始化數據模型。
  • NSError* error = nil;CountryModel* countryModel = [[CountryModel alloc] initWithDictionary:dict error:error];

    如果JSON的驗證通過你在你的模型中填充的JSON所有相應的屬性。JSONModel也將嘗試將盡可能多的數據轉換為您所期望的類型,在這個例子中,dialCode將數字轉換成字符串。

    網絡請求數據轉換模型

    接口:https://news-at.zhihu.com/api/4/version/ios/2.3.0

    返回數據示例:

    { "status":1, "msg":"【更新內容】\r\n\r\n★ 多圖有標記 流量壕忽略\r\n★ 出門前離線 沒網也能看\r\n★ 喜歡請好評 不喜快吐槽\r\n★ 萌妹工程師 郵箱在下面\r\nmua@zhihu.com\r\n(一般人我們不告訴他)", "latest":"2.5" }

    Objective-C類:

    @interface TestModel : JSONModel @property (nonatomic, assign) int status; @property (nonatomic, copy) NSString* msg; @property (nonatomic, copy) NSString* latest; @end

    請求數據:

    - (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.NSString* string = [NSString stringWithFormat:@"https://news-at.zhihu.com/api/4/version/ios/2.3.0"];string = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];NSURL* url = [NSURL URLWithString:string];NSURLRequest* request = [NSURLRequest requestWithURL:url];NSURLSession* session = [NSURLSession sharedSession];NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {TestModel* country = [[TestModel alloc] initWithData:data error:nil];NSLog(@"%@", country);}];[dataTask resume]; }

    結果:

    集合、嵌套型數據

    接口:https://news-at.zhihu.com/api/4/news/latest

    返回數據示例:

    {"date":"20221015","stories":[{"image_hue":"0xa08861","title":"小事 · 外婆的「江湖」","url":"https:\/\/daily.zhihu.com\/story\/9754027","hint":"VOL.1445","ga_prefix":"101507","images":["https:\/\/pic2.zhimg.com\/v2-c54a943ca0074ad6f85c3935d4205ce8.jpg?source=8673f162"],"type":0,"id":9754027},{"image_hue":"0x9c3b4b","title":"超長篇哆啦 A 夢漫畫哪一部水平最高?","url":"https:\/\/daily.zhihu.com\/story\/9753999","hint":"Shimmer · 9 分鐘閱讀","ga_prefix":"101507","images":["https:\/\/pic2.zhimg.com\/v2-6a2b729e0ffc66150ebf865aed19cadd.jpg?source=8673f162"],"type":0,"id":9753999},{"image_hue":"0x311a2b","title":"據說宇宙大爆炸之前沒有時間與空間,「沒有時間、空間」是什么意思?","url":"https:\/\/daily.zhihu.com\/story\/9754006","hint":"王清揚 · 3 分鐘閱讀","ga_prefix":"101507","images":["https:\/\/pic1.zhimg.com\/v2-7b449549c16e7aa1757043c10ff51aaf.jpg?source=8673f162"],"type":0,"id":9754006},{"image_hue":"0x1b3533","title":"近視眼在老了以后真的會恢復并且中和老花眼嗎?","url":"https:\/\/daily.zhihu.com\/story\/9754008","hint":"中科院物理所 · 1 分鐘閱讀","ga_prefix":"101507","images":["https:\/\/pic2.zhimg.com\/v2-352360b6eb73711a9b5ef1f76c1894e5.jpg?source=8673f162"],"type":0,"id":9754008},{"image_hue":"0x3b2f25","title":"中世紀的教士是如何斂財和升職的?","url":"https:\/\/daily.zhihu.com\/story\/9754018","hint":"萊茵行宮伯爵 · 10 分鐘閱讀","ga_prefix":"101507","images":["https:\/\/pica.zhimg.com\/v2-d7c8282951f182efcbde644bd3b5d85f.jpg?source=8673f162"],"type":0,"id":9754018},{"image_hue":"0x472d1b","title":"10 × 10 的正方形最多可放入多少個直徑為 1 的圓?","url":"https:\/\/daily.zhihu.com\/story\/9754022","hint":"醬紫君 · 3 分鐘閱讀","ga_prefix":"101507","images":["https:\/\/pic1.zhimg.com\/v2-7687a394ff65690621f2934ea0fe91c3.jpg?source=8673f162"],"type":0,"id":9754022}],"top_stories":[{"image_hue":"0xa08861","hint":"作者 \/ 姜喵","url":"https:\/\/daily.zhihu.com\/story\/9754027","image":"https:\/\/pic1.zhimg.com\/v2-6c638b9f5b35c93c0e5c6ecb5a30611a.jpg?source=8673f162","title":"小事 · 外婆的「江湖」","ga_prefix":"101507","type":0,"id":9754027},{"image_hue":"0x040505","hint":"作者 \/ 王治鈞Lawrence","url":"https:\/\/daily.zhihu.com\/story\/9753968","image":"https:\/\/pic2.zhimg.com\/v2-b55613fa89bf75a9c2dc6255cb240d96.jpg?source=8673f162","title":"你經歷或見證過哪些「曼德拉效應」事件?","ga_prefix":"101407","type":0,"id":9753968},{"image_hue":"0x40372d","hint":"作者 \/ 豆子","url":"https:\/\/daily.zhihu.com\/story\/9753873","image":"https:\/\/picx.zhimg.com\/v2-c8e1fa94c269e7318a10ea1562dba69b.jpg?source=8673f162","title":"古代的丫鬟每天都做什么?","ga_prefix":"101007","type":0,"id":9753873},{"image_hue":"0x919399","hint":"作者 \/ 知乎用戶","url":"https:\/\/daily.zhihu.com\/story\/9753797","image":"https:\/\/picx.zhimg.com\/v2-4fa5e85fe83822f33d039e4cacc77598.jpg?source=8673f162","title":"為什么飯店的大鐵鍋從來都不粘鍋?","ga_prefix":"100907","type":0,"id":9753797},{"image_hue":"0x121924","hint":"作者 \/ 瞻云","url":"https:\/\/daily.zhihu.com\/story\/9753754","image":"https:\/\/picx.zhimg.com\/v2-ef9dfc5cb5b221cf487d878f19133cd9.jpg?source=8673f162","title":"如果沒有生殖隔離地球會變成什么樣?","ga_prefix":"100807","type":0,"id":9753754}] }
    • 對于這種數據,我們需要將每一個嵌套都寫成一個類。這些我們可以全部在一個文件中實現。
    @protocol StoriesModel @end@protocol Top_StoriesModel @end#import <UIKit/UIKit.h>#import "JSONModel.h"NS_ASSUME_NONNULL_BEGIN@interface StoriesModel : JSONModel @property (nonatomic, copy) NSString* title; @property (nonatomic, copy) NSString* ga_prefix; @property (nonatomic, copy) NSString* image_hue; @property (nonatomic, copy) NSString* type; @property (nonatomic, assign) NSString* id; @end@interface Top_StoriesModel : JSONModel @property (nonatomic, copy) NSString* title; @property (nonatomic, copy) NSString* ga_prefix; @property (nonatomic, copy) NSString* image_hue; @property (nonatomic, copy) NSString* type; @property (nonatomic, assign) NSString* id;@end@interface TestModel : JSONModel@property (nonatomic, copy) NSString* data; @property (nonatomic, copy) NSArray<StoriesModel>* stories; @property (nonatomic, copy) NSArray<Top_StoriesModel>* top_tories;//@property (nonatomic, assign) int status; //@property (nonatomic, copy) NSString* msg; //@property (nonatomic, copy) NSString* latest; @endNS_ASSUME_NONNULL_END

    .m文件中:

    #import "TestModel.h"@implementation StoriesModel + (BOOL) propertyIsOptional:(NSString *)propertyName {return YES; } @end@implementation Top_StoriesModel + (BOOL) propertyIsOptional:(NSString *)propertyName {return YES; } @end@implementation TestModel + (BOOL) propertyIsOptional:(NSString *)propertyName {return YES; } @end

    發送網絡請求:

    - (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.NSString* string = [NSString stringWithFormat:@"https://news-at.zhihu.com/api/4/news/latest"];string = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];NSURL* url = [NSURL URLWithString:string];NSURLRequest* request = [NSURLRequest requestWithURL:url];NSURLSession* session = [NSURLSession sharedSession];NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {TestModel* country = [[TestModel alloc] initWithData:data error:nil];NSLog(@"%@", country.stories[0]);}];[dataTask resume]; }

    結果:

    總結

    以上是生活随笔為你收集整理的【OC】JSONModel基本使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。