Gson equivalent in Objective-C

后端 未结 8 1453
我寻月下人不归
我寻月下人不归 2021-02-05 09:14

Is there any equivalent to gson in Objective-C?

Thanks.

相关标签:
8条回答
  • 2021-02-05 09:51

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

    https://github.com/Mantle/Mantle

    0 讨论(0)
  • 2021-02-05 09:53

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

    0 讨论(0)
  • 2021-02-05 09:55

    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.

    0 讨论(0)
  • 2021-02-05 09:59

    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];
    
    0 讨论(0)
  • 2021-02-05 09:59

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

    0 讨论(0)
  • 2021-02-05 10:01

    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

    0 讨论(0)
提交回复
热议问题