Jsonmodel is a library that can intelligently and quickly create data model that you can use on your IOS project or OS X project.
How to use Jsonmodel
1* create a class of your own and inherit to Jsonmodel
2. Declare the JSON key value you need in your header file
You don't have to do anything else in the 3*. m file.
Next, briefly introduce some of the general rules of use of Jsonmodel.
1. Jsonmodel with a validation check, if the specified server returns a field that does not return a value, and it must, as written below, throws an exception
@property (nonatomic, strong) NSString *nothing;
In general, we don't want to cause the program to crash because a value of the server does not return, we add the keyword optional.
@property (nonatomic, strong) NSString <Optional>*Nothing;
2. If the JSON is valid, all of the attributes you define will match the JSON value, and Jsonmodel will try to convert as much of the data as you want:
Originally, the Nsinteger type was directly converted to NSString
@property (nonatomic, strong) NSString *city_id;
3 automatically the name of the underline way into the hump named attribute. There are similar methods for converting uppercase to lowercase
@property (nonatomic, strong) NSString *city_name;
Write this method in the. m file
+ (jsonkeymapper*) keymapper {return
[jsonkeymapper mapperfromunderscorecasetocamelcase];
}
@property (nonatomic, strong) NSString *cityname;
Jsonmodel Processing Data problems
Request URL: http://api.lanrenzhoumo.com/district/list/allcity?session_id=00004016b3e14bbea40c1aa1a14c2273a35352
Request data type
Way to
create a model inheritance Jsonmodel declare the key you want when parsing the data, we can use the following method to create the mode
ambituscitytypemodel *model = [ Ambituscitytypemodel alloc] Initwithdictionary:dic Error:nil];
The Initwithdictionary method
is equivalent to creating model KVC Assignment//mode two//In normal parsing the array in which the model is
created according to the dictionary array is a model type of data
Self.dataarray = [Loanmodel Arrayofmodelsfromdictionaries:temparr];
Jsonmodel to deal with complex nesting problems with the above data and interface examples
Declaration two model this nested data is the #import "JSONModel.h" @protocol ambituscitymodel @end @interface Ambituscitymodel:jsonmodel @
Property (Nonatomic, assign) Nsinteger city_id;
@property (nonatomic, strong) NSString *city_name; @end #import "AmbitusCityModel.h" @implementation Ambituscitymodel @end//second model #import "JSONModel.h" #import "Ambi
TusCityModel.h "@interface Ambituscitytypemodel:jsonmodel @property (nonatomic, strong) NSString *begin_key;
@property (nonatomic, strong) nsmutablearray<ambituscitymodel> *city_list; @end #import "AmbitusCityTypeModel.h" @implementation Ambituscitytypemodel @end//parsing process is as follows-(void) SetupData {Nsstri ng *urlstring = [NSString stringwithformat:@ "%@", @ "http://api.lanrenzhoumo.com/district/list/allcity?session_id=
00004016b3e14bbea40c1aa1a14c2273a35352 "]; Nsmutableurlrequest *request = [[Nsmutableurlrequest alloc] Initwithurl:[nsurl urlwithstring:urlstring] CachePolicy: (
Nsurlrequestuseprotocolcachepolicy) timeoutinterval:30.0]; [Request sethttpmethod:@ "get"];
Self.dataarray = [Nsmutablearray array];
Nsurlsessionconfiguration *config = [Nsurlsessionconfiguration ephemeralsessionconfiguration];
Nsurlsession *session = [Nsurlsession sessionwithconfiguration:config];
Self.dataarray = [Nsmutablearray array]; Nsurlsessiondatatask *datatask = [Session datataskwithrequest:request completionhandler:^ (NSData * _Nullable data, Nsurlresponse * _nullable response, Nserror * _nullable error) {if (error) {NSLog (@ "Httperror:%@%ld
", Error.localizeddescription, Error.code); else {if (data) {nsdictionary *tempdict = [Nsjsonserialization jsonobjectwithdata:data opt
Ions: (nsjsonreadingmutablecontainers) Error:nil];
Self.dataarray = [Typetwomodel arrayofmodelsfromdictionaries:[tempdict valueforkey:@ "result"]];
Dispatch_async (Dispatch_get_main_queue (), ^{[Self.tableview reloaddata]; });
}
}
}];
[Datatask resume]; }