When an iPhone app receives data as either JSON or XML is it better to store the data in an nsdictionary or a mutable array?
-
If building an iPhone app that needs to take in complex data from a web service. Would it be better for the data coming from the web service be in JSON or XML? Anybody that can post a link with tutorials on any of this would be a true angel. The "web service" will be in PHP... have already used jsonencode but wanted to get other options that could store more complex data.
-
Answer:
When building an iPhone app, the first thing you want to do in minimize the amount of data going over the network connection, as it's the slowest part of the application. JSON is a far more compact format than XML because it contains only data, no structural elements or tags. Which is why I always use it. Secondly, A JSON parser converts the JSON into whatever format is needed in memory for access, so if the JSON has an Array of Strings, say, it creates an NSArray of Strings, if the JSON has a hash of key-value pairs, it creates a NSDictionary for you. And if it has a hash of arrays of strings, it creates an NSDictionary of NSArrays of strings. It just works! No matter how complex the JSON, the parser creates a sane, in memory, navigable data structure for you. Final point, if you receive data from the internet, always use immutable data structures. They use less memory and you are less likely to 'corrupt' the data while accessing it and introduce hard to find bugs. If you need to change the data and send it back, make a mutable copy.
Hilton Lipschitz at Quora Visit the source
Other answers
JSON is more compact than XML and it's far easier to work with in iOS, because all you have to do is create an instance of NSJSONSerialization with some data and then BOOM, you've got a Dictionary (or Array) to work with. If you went the XML route, you'd have to use NSXMLParser, and it's a big pain in the ass to load up a Dictionary/Array because you have to write a lot of event-driven parsing code. As for whether to go with NSDictionary or NSMutableArray... well, first, I think you mean NSDictionary or NSArray. Data you're getting from the internet probably shouldn't be changed on your end, so you probably would never want to create a Mutable array. If you did, you'd probably have a higher level abstraction which would hide the underlying collection. Whether or not you use a Dictionary or Array depends on your app needs (and is decided for you by NSJSONSerialization anyway).
Brian Papa
In addition to what has said. If you're developing for iOS 5 you can use the built in library NSJSONSerialization to parse your JSON encoded data. It will return a NSData object that you can cast into NSDictionary or NSArray. NSJSONSerialization documentation can be found here: http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html
Mohab Samman
Between XML and JSON I will vote for JSON because It contains Only data and can create data type like NSDictionary, NSArray as mentioned by some devs above. And you dont need to use 3rd party frameworks like SBJSON or other because iOS 5 has built-in library NSJSONSerialization for JSON.
Hel Galib
Like Hilton said, JSON is more compact, and if use the SBJSON parser, it automatically parses and stores the data for you as KVO objects in an NSDictionary. Amongst developers, SBJSON is the most widely accepted JSON-parser for iOS 4 and below. With the introduction of iOS 5, Apple has provided developers with the NSJSONSerialization class that takes care of all the legwork for you. SBJSON can be foud on GitHub: http://stig.github.com/json-framework/
Arthur Sabintsev
Related Q & A:
- Is Using Gmail to Store Application Data a Violation of their Terms of Service?Best solution by Quora
- What's your favorite ipod/iphone app?Best solution by Yahoo! Answers
- What is your favorite Ipod Touch/iPhone app?Best solution by Quora
- What is your favourite iPhone app?Best solution by Yahoo! Answers
- What's so significant about Google Voice as an iPhone app?Best solution by Quora
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.