Which json library to use on iphone to consume gson/jackson generated json
Asked Answered
S

4

8

I need to build a java based JSON data API that will be accessed from mobile devices: Android, Iphone... I have narrowed down to these:

  • Server side data api - gson/jackson lib (pojo to json)
  • Android side - gson/jackson lib (json to pojo)
  • Iphone side - I googled & found that there are a bunch of frameworks Jsonkit, json-framework, touch json etc etc.

Question:

  • Can someone make a recommendation on the iphone side json library? The gson/jackson generated json output of the data api, must be consumable from iphone without any special tweaks to json structure

  • Is there any java Collection type that I should stay away from(on server side) to ensure the json is directly convertible to objective c objects?

  • Any other gotchas?

Note: I am aware of PhoneGap, titanuim & some equivalents - but not interested in them. Json is meant to be a universal format, but I have had cross-library issues earlier - hence this attempt to understand from people with prior experiance on the subject

Thanks!

Siloxane answered 25/10, 2011 at 16:22 Comment(0)
E
8
  • I've been using RestKit Library in an iPhone App Project. It is a nice framework that supports HTTP request/response handling with (mostly REST) servers, JSON/XML parsers to help you with data translation and a JSON <-> Object Mapping system.

    It may supply your iPhone server communication needs (making requests to the server and receiving JSON responses from server and converting each of them into objective-c objects).

  • For RestKit, it only matters the objects of your JSON file. So you are free to use any class or collection in your server as far as you generate a valid JSON file as response.

  • It took me some time to learn how to use RestKit properly in my applications. But when you get used to it, it definitely saves you time, because it hides you the complexity of parsing JSON files for every request made to server. You are right in trying to pick a good JSON data handling API for your application.

    Another good thing about RestKit is its community. It has a GitHub, a Google Groups and an IRC channel. If you plan to use it as a framework, assigning to its mailing list is definitely a good idea.

Have a nice coding! ;P

Electromagnetic answered 25/10, 2011 at 17:32 Comment(5)
I succeeded in consuming my webservice using RestKit. Sorry I got sucked into other things & took a while to get back. But yes it works & works well! Thanks again!.Siloxane
Nice to know it was helpful and you succeeded! You have to thank RestKit community, not me ^^... but anyway, you're welcome :)Electromagnetic
I found that RestKit required a ton of boilerplate mapping, even for simple objects. Here's another option if you want something that works more like Gson or Jackson: https://mcmap.net/q/1327568/-jackson-equivalent-for-iphoneHightail
Thanks @EdwinVermeer . I've updated the link. RestKit is now at Github.Electromagnetic
@AlexNauda, nice to know about that. I really like the convention over configuration idea. This post is a bit old and nowadays there could be other nice libraries to look at.Electromagnetic
D
2

One of the best libraries to parse JSON in swift is EVReflection. It offers a very easy installation using CocoaPods or you can just download it and add to your project.

Usage is very easy and it works like GSON or Jackson in Android/Java.

Example using Alamofire:

class User : EVObject{
    // EVReflection does not support optionals!
    // So you have to not declare any optional vars
    var name = ""
    var surname = ""
}

Alamofire.request("Your API URL").responseString{
    response in
        let resString = response.result.value
        // assuming that resString = "{'name' : 'john', 'surname' : 'Doe'}"
        let user = User(json : resString)
}
Domitian answered 18/1, 2017 at 14:15 Comment(1)
EVReflection does support optionals. There is only an issue with optional Bool and Int. Optional String and optional Number? If you do want to use optional Int and Bool, then you can use the workaround that is shown in the 'WorkaroundUnitTests'Brocade
A
1

Personally I've been using the json-framework by Stig for my iOS development and works great. you can download it here.

Accentuate answered 25/10, 2011 at 16:30 Comment(0)
D
0

OCMapper is similar to GSON, it has the ability to automatically map all fields and is simple to use. It supports both objective C and Swift:

let request = Manager.sharedInstance.request(requestWithPath("example.com/users/5", method: .GET, parameters: nil))

request.responseObject(User.self) { request, response, user, error in
    println(user.firstName)
}
Dammar answered 15/6, 2015 at 0:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.