18

Is there any equivalent to gson in Objective-C?

Thanks.

itsaboutcode
  • 24,525
  • 45
  • 110
  • 156

8 Answers8

14

DCKeyValueObjectMapping https://github.com/dchohfi/KeyValueObjectMapping is not a JSON parser but an object-json mapper to inject NSObject properties from NSDictionary/NSArray.

FUJI Goro
  • 869
  • 10
  • 19
6

I recently used Mantle which works great and is very similar to GSON (which is use for android projects)

https://github.com/Mantle/Mantle

Mads Lee Jensen
  • 4,570
  • 5
  • 36
  • 53
5

In Objective-C the functionality of GSON is sort of built in. Say I have a class defined like so:

 @interface MyModel : NSObject
 @property(nonatomic,strong) NSString *name;
 @property(nonatomic,strong) NSString *address;
 @end

And lets say that I have a JSON object defined like so

{
    "name":"marc",
    "address":"1234 Some Street"
}

Then I can use AFNetowrking to get an NSDictionary of the JSON object which is pretty easy. Finally you can just do a loop like so where dict is the dictionary returned by AFNetworking parsing the JSON and self is an instance of MyModel.

for (NSString *key in dict) {
   [self setObject:dict[key] forKey:key];
}

In Java GSON uses reflection to achieve the same effect as the above loop. Its just a lot easier in objective-c so no need for a library to do it. If you have nested objects maybe AFNetworking with DCKeyValueObjectMapping.

Marc
  • 1,159
  • 17
  • 31
  • in android gson uses reflection but reflection is slow in android. We all still use GSON for deserilization though. I was wondering for iOS is it a slow down to map using AFNetworking ? – j2emanue Mar 07 '18 at 10:13
4

OCMapper is very similar to Gson and easy to use https://github.com/aryaxt/OCMapper

Json

{
   "firstName"   : "FirstName",
   "lastName"    : "LastName",
   "age"         : 26,
   "dateOfBirth" : "01/01/2013",
   "address"     : { 
                        "city" : "San Diego", 
                        "country" : "US"  
                   },
   "posts"       : [
                         {
                             "title" : "Post 1 title",
                             "datePosted : "04/15/2013",
                         },
                         {
                             "title" : "Post 2 title",
                             "datePosted : "04/12/2013",
                         }
                   ]
}

Model

@objc public class User: NSObject {

    var firstName: String?
    var lastName: String?
    var age: NSNumber?
    var dateOfBirth: NSDate?
    var address: Address?
    var posts: [Post]?
}

Usage Swift

let user = ObjectMapper.sharedInstance().objectFromSource(dict, toInstanceOfClass:User.self) as User

or

let User = User.objectFromDictionary(dictionary)

Usage Objective C

User *user = [[ObjectMapper sharedInstance] objectFromSource:dictionary toInstanceOfClass:User.class];

or

User *user = [User objectFromDictionary:dictionary];
aryaxt
  • 76,198
  • 92
  • 293
  • 442
0

At WWDC 2017, Apple has introduced the new feature in Swift to parse JSON without any pain using Swift Codable protocol

struct YourStructure: Codable {
    let name: String?
    let avatarUrl: URL?
    private enum CodingKeys: String, CodingKey {
        case name
        case avatarUrl = "avatar_url"
    }
}

decoder:

let decoder = JSONDecoder()
parsedData = decoder.decode(YourStructure.self, from: YourJsonData)

encode:

let jsonEncoder = JSONEncoder()
let jsonData = try jsonEncoder.encode(data)

more info: Encoding and Decoding Custom Types

phnmnn
  • 12,813
  • 11
  • 47
  • 64
0

OCMapper is the best i know and the easiest library and it have reverse mapping as well and map complex objects without the need of configuration , and work with realmObjects as well

youssef ali
  • 406
  • 5
  • 11
-2

Yes - see http://psionides.jogger.pl/2010/03/04/cocoa-json-parsing-libraries/

Jason
  • 14,517
  • 25
  • 92
  • 153
-2

I think I have found few libraries which can server this purpose but most important one seems to be RestKit

itsaboutcode
  • 24,525
  • 45
  • 110
  • 156
  • 6
    We used RestKit on a project and I would never use that library again. It was super slow and responsible for a lot of weird bugs. – Marc Mar 14 '15 at 17:42
  • 4
    Using RestKit is like digging your own grave. – aryaxt May 17 '15 at 22:11
  • @Marc is using Mantle better then others? I am new to iOS so don't have any idea about any library. – Chintan Rathod Sep 23 '15 at 12:52
  • @ChintanRathod Use AFNetworking. In Objective-C no need for mapping lib because you can use [setObject:forKey:] on class properties. So rolling your own is trivial. – Marc Sep 23 '15 at 20:24