tags:

views:

328

answers:

5

I am starting to create an iPhone application that will interact with a public API.

My question is, which will be faster and/or easier to work with: XML or JSON?

+1  A: 

Can't tell with so little information. It depends on the quality and capabilities of the libraries on either end that deal with that data form.

But you already know that JSON will be more compact, because it doesn't have to deal with closing tags (variables and open tags seem a wash to me). Fewer bytes on the wire for JSON.

duffymo
+3  A: 

In terms of built-in APIs, to my knowledge, there's no native support for JSON, so XML would be the only choice if you didn't want to use external libraries.

That said, JSON tends to be much easier to work with because it synthesizes directly into Cocoa objects(i.e. NSDictionary, NSArray, NSString, NSNumber), so I would say JSON is much easier to work with if you're willing to use an external library, and you have good server-side support for JSON. I've had good luck with the json-framework library, so my advice is to give that a try.

Another bonus of JSON is that it typically(almost always) will produce smaller file transfer sizes over the network because of its formatting. While saving a few kb might be overlooked, it is a major win on a mobile device with limited bandwidth while on a cell network.

Mike
I like JSON, but I use TouchJSON http://code.google.com/p/touchcode/wiki/TouchJSON
gerry3
+5  A: 

According to this post, TouchJSON outperforms XML property lists. However, that is a single data point.

Brad Larson
+1  A: 

JSON is quite alot terser which saves bandwidth and makes processing more efficient, so i'd choose that.

Paul Creasey
A: 

I would also use JSON, with TouchJSON (as suggested earlier). The payload is smaller and the parsing is faster in my own tests. Oh, and by the way, don't use NSXMLParser, it's really slow. Use libxml2 or TouchXML instead. Check out this article about a direct experience with JSON on the iPhone:

Another possible approach is to generate XML or binary Property Lists, and then deserializing that into native objects. Your server-side application might generate the plist format for you using plistlib on Python or plist for Ruby.

Adrian Kosmaczewski