Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jamztang/JTObjectMapping
A very simple objective-c framework that maps a JSON response from NSDictionary or NSArray to an NSObject subclass for iOS.
https://github.com/jamztang/JTObjectMapping
Last synced: 3 months ago
JSON representation
A very simple objective-c framework that maps a JSON response from NSDictionary or NSArray to an NSObject subclass for iOS.
- Host: GitHub
- URL: https://github.com/jamztang/JTObjectMapping
- Owner: jamztang
- License: mit
- Created: 2011-09-06T09:11:32.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2016-01-15T08:01:53.000Z (about 9 years ago)
- Last Synced: 2024-10-11T14:17:30.035Z (4 months ago)
- Language: Objective-C
- Homepage:
- Size: 127 KB
- Stars: 263
- Watchers: 27
- Forks: 56
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome - JTObjectMapping - A very simple objective-c framework that maps a JSON response from NSDictionary or NSArray to an NSObject subclass for iOS. (etc)
- awesome - JTObjectMapping - A very simple objective-c framework that maps a JSON response from NSDictionary or NSArray to an NSObject subclass for iOS. (etc)
README
JTObjectMapping
===============Inspired by RestKit. A very simple objective-c framework that maps a JSON response from NSDictionary or NSArray to NSObject subclasses for iOS.
Install
-------### Original method
Copy all files in JTObjectMapping/ into your project.
### CocoaPods
`$ pod search JTObjectMapping`, you should be able to specify the right version in your Podfile. Here's more information about [CocoaPods][].
Usage
-----
Suppose this is a JSON User object response represented in NSDictionary after parsing
{
"create_date" = "1970-01-01T00:00:00+0000";
"p_age" = 30;
"p_childs" = (
Mary,
James
);
"p_name" = Bob;
"p_title" = Manager;
"social_networks" = {
"twitter" = "@mystcolor";
"facebook" = "yourFacebookID";
}
}Get ready with your JSON use **[NSObject objectFromJSONObject:json mapping:mapping]** to convert.
...
NSDictionary *json = ;//
// Use +[NSObject objectFromJSONObject:mapping:] to convert
// the NSDictionary into your JTUserTest object
//
JTUserTest *user = [JTUserTest objectFromJSONObject:json mapping:mapping];
...Define necessary mappings, from a dictionary key to a property keyPath.
// Define the mapping of a nested custom object - JTSocialNetworkTest
NSDictionary *socialNetworkMapping = [NSDictionary dictionaryWithObjectsAndKeys:
@"twitterID", @"twitter",
@"facebookID", @"facebook",
nil];NSDictionary *mapping = [NSDictionary dictionaryWithObjectsAndKeys:
@"name", @"p_name",
@"title", @"p_title",
@"age", @"p_age",
@"childs", @"p_childs",
[NSDate mappingWithKey:@"createDate"
dateFormatString:@"yyyy-MM-dd'T'HH:mm:ssZ"], @"create_date",
[JTSocialNetworkTest mappingWithKey:@"socialNetwork"
mapping:socialNetworkMapping], @"social_networks",
nil];Of course you need to define your own User object with corresponding @synthesize properties, and thats all for what you need.
// JTSocialNetworkTest.h
@interface JTSocialNetworkTest
@property (nonatomic, copy) NSString *twitter;
@property (nonatomic, copy) NSString *facebook;
@end// JTSocialNetworkTest.m
@implementation
@end// JTUserTest.h
@interface JTUserTest : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSNumber *age;
@property (nonatomic, strong) NSDate *createDate;
@property (nonatomic, strong) NSArray *childs;
@property (nonatomic, strong) JTSocialNetworkTest *socialNetwork;
@end
// JTUserTest.m
#import "JTUserTest.h"
@implementation JTUserTest
@endFor more detailed usage, see **JTObjectMappingTests.m**, will be adding more detailed description soon.
Update Logs
-----------v1.1.2
- Added auto mapping from underscores to CamelCases (e.g. full\_name -> fullName)v1.1.1
- Added URL support, thanks to [@TheSantaClaus][] and adding the test
cases nicely.v1.1
- Refactored JTObjectMapping. Now extending custom mappings are much more cleaner.
- Proper keyPath support.v1.0.7
- Added JTSetMapping and JTDateEpochMappings, thanks to [@zcharter][] for making this happen!v1.0.6
- Added experimental keypath support. use `#define JTOBJECTMAPPING_DISABLE_KEYPATH_SUPPORT = 1` to disable it.v1.0.5
- Fixed nested array causing crashv1.0.4
- Added auto NSDictionary value to NSObject property mapping with the same key defined
- Fixed false possible JSON response in NSArray use casev1.0.3
- Add raw array JSON response supportv1.0.2
- Added NSArray supportv1.0.1
- Added NSDate support for mappings[CocoaPods]:https://github.com/CocoaPods/CocoaPods
[@zcharter]:https://github.com/zcharter
[@TheSantaClaus]:https://github.com/TheSantaClaus[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/jamztang/jtobjectmapping/trend.png)](https://bitdeli.com/free "Bitdeli Badge")