• Stars
    star
    263
  • Rank 155,624 (Top 4 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created about 13 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A very simple objective-c framework that maps a JSON response from NSDictionary or NSArray to an NSObject subclass for iOS.

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 = <Parsed JSON response from above>;

//
// 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
@end

For 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 crash

v1.0.4

  • Added auto NSDictionary value to NSObject property mapping with the same key defined
  • Fixed false possible JSON response in NSArray use case

v1.0.3

  • Add raw array JSON response support

v1.0.2

  • Added NSArray support

v1.0.1

  • Added NSDate support for mappings

Bitdeli Badge

More Repositories

1

JTGestureBasedTableViewDemo

(demo) Recreating the buttonless interaction pattern found in Clear for iPhone app
Objective-C
1,332
star
2

JTRevealSidebarDemo

(demo) A carefully implemented iOS objective-c library to mimic the sidebar layout of the new Facebook app and Path 2.0 app.
Objective-C
1,178
star
3

CRDT

CRDT implementation in Swift, Last Write Win
Swift
42
star
4

AFNetworking-ProxyQueue

AFNetworking with multiple operation queue support, to separate download operations from affecting too much on main network request.
Objective-C
40
star
5

JTAttributedLabel

Ported back NSAttributeString in iOS 5 for you to create rich text UILabels in Interface Builder. (experimental)
Objective-C
38
star
6

MVCP

Introducing Model-View-Controller-Presenter
Objective-C
31
star
7

ObjectiveSpring

A sample project for using Spring in Objective-C project
Swift
28
star
8

SettingsBundleDemo

Integrate Settings.bundle and show iOS version with Git commit in 5 mins
Objective-C
25
star
9

JTPinIt

An incomplete project which aimed to provide share to Pinterest without going through that in-app camera which crops your image
Objective-C
13
star
10

JTImageKit

Simple cocoa library to provide convenience image manipulation for iOS
Objective-C
7
star
11

Today-PressKit

Press Kit for Today iOS app
5
star
12

Today-web

Landing Page for Today iOS App
CSS
3
star
13

JTImageProxyDemo

Objective-C
3
star
14

JTJSON

Just an experimental project of finding out the possibilities to get a JSON response to be translated to a plist format. Do we really need a 3rd party string parsing based JSON library?
Objective-C
3
star
15

ga-bridge

Use Google Analytics without SDK (and for your OSX application)
Objective-C
2
star
16

mystcolor.github.com

Ruby
2
star
17

XcodeProjectTemplateForSketchPlugin

Too bad, don't use.
Objective-C
2
star
18

JTOperationWallet

An objective-c NSProxy based library for simulating what blocks are convenience for, an iOS 3 compatible alternative solution to blocks.
Objective-C
2
star
19

OpenRxSwift

WIP
Swift
1
star
20

jamztang.github.io

JavaScript
1
star
21

CropTool

Swift
1
star
22

ioscodesnippet

1
star
23

testTrackpadGesture

Swift
1
star
24

testHoverButton

ToolTip implementation for Catalyst
Swift
1
star
25

DynamicNavigation

Swift
1
star
26

IntentionKit

Solely experiment and not under active development
Objective-C
1
star
27

prototype

Presentation demo project for "Prototyping iPhone app with Xcode and Storyboard"
Objective-C
1
star