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

7.9k views Asked by At

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!

4

There are 4 answers

5
hbobenicio On BEST ANSWER
  • 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

0
aryaxt On

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)
}
0
Alan Moore On

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

1
Medin Piranej On

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)
}