开发者

Which is faster/easier to work with in an iPhone app: XML or JSON?

开发者 https://www.devze.com 2022-12-15 02:45 出处:网络
I am starting to create an iPhone application that will interact with a public API. My q开发者_如何学运维uestion is, which will be faster and/or easier to work with: XML or JSON?According to Sam Soff

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

My q开发者_如何学运维uestion is, which will be faster and/or easier to work with: XML or JSON?


According to Sam Soffes, TouchJSON outperforms XML property lists:

When I was preparing for my talk I decided to do some benchmarks to show how much more awesome plists were than JSON. The plist version was about 8 times faster than my JSON Framework version. I was pretty happy with that result. My friend, Jake, said he was using TouchJSON to parse JSON in his apps, so I figured I’d go ahead and benchmark that one too. I was expecting JSON Framework to beat it because the interface to the JSON Framework is a lot simpler than the interface to TouchJSON.

What I found was very surprising. TouchJSON actually beat plists. It was slightly faster in every test I ran. This is awesome because plists have a much larger file size. They are usually about twice as big as JSON files due to all of the extra markup.

However, that is a single data point.


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.


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.


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


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:

  • http://samsoff.es/post/web-services-with-cocoa-surprise/

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消