Mjextension
To make a long story short, let's see how we use a column
1. Pull the frame into your project first
2, first of all I use the JSON outermost is a dictionary, according to the model of the data we can classify this as an array in the dictionary, there are other in the array for this situation I need to build two data model one is to get the array of the other array in the city element
Explain the idea behind the code
#import <Foundation/Foundation.h>
@interface Nscity:nsobject
@property (Nonatomic,strong) NSString *city;
@property (Nonatomic,strong) NSString *h5_url;
@property (Nonatomic,strong) nsnumber* ID;
@property (Nonatomic,strong) NSString *deal_url;
@end
Nscity.m
mjextension Example
//
Created by Tareba on 15/12/18.
COPYRIGHT©2015 year Tanada. All rights reserved.
//
#import "NSCity.h"
@implementation nscity
-(void) SetValue: (ID) value Forundefinedkey: (NSString *) Key {
if ([Key isequaltostring:@ "id"]) {
Self.id=value;
}
}
@end
#import <Foundation/Foundation.h>
@interface Nsdeals:nsobject
@property (Nonatomic,strong) Nsmutablearray *deals;
@end
#import "NSDeals.h"
#import "MJExtension.h"
@implementation Nsdeals
The following methods are key
、、
+ (Nsdictionary *) Mj_objectclassinarray
{
Return @{
@ "Deals": @ "Nscity",
};
}
@end
#import "ViewController.h"
#import "MJExtension.h"
#import "NSCity.h"
@interface Viewcontroller ()
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Nsurlrequest *request =[nsurlrequestrequestwithurl:[nsurlurlwithstring:@ "Http://api.dianping.com/v1/deal/find_ Deals?appkey=4123794720&category=%e7%be%8e%e9%a3%9f&city=%e9%83%91%e5%b7%9e®ion=%e9%87%91%e6%b0 %b4%e5%8c%ba&sort=2&sign=fb7a61eb742a4b697b666985348614c7a8f763a3 "];
Nsurlsession *seesion = [Nsurlsessionsharedsession];
Nsurlsessiondatatask *task =[seesiondatataskwithrequest:request completionhandler:^ (NSData * _Nullable data, Nsurlresponse * _nullable response, Nserror * _nullable error) {
Nsdictionary *dic=[nsjsonserialization JSONObjectWithData:dataoptions:NSJSONReadingMutableContainers Error:nil];
Nsdeals *result =[nsdealsmj_objectwithkeyvalues:dic];
For (nscity *city inresult.deals) {
NSLog (@ "%lu", (unsigned long) result.deals.count);
NSLog (@ "%@", City.deal_url);
}
}];
[Task resume];
}
@end
This method is to change the hierarchical relationship of JSON data into an attribute relationship, the next layer becomes the property of the previous layer, and then the property can be accessed directly by the elder sister. As for the keyword to become a property, due to the urgency of the future and the validation and the usual use of setvaluefor Undefinekey to resolve whether the same will be verified as soon as possible after the modification
Mjextension JSON Quick Parse