Parsing objects in Json array as a Swift dictionary
So, I have JSON data that is formatted as a list of dictionaries and I saved it as an NSArray object, but I'm not sure how to convert each entry to a dictionary object when it is currently AnyObject
AnyObject data is already formatted as a JSON dictionary
Here is the code I used to create the array
func startConnection(){
let urlPath: String = "http://api.mtgdb.info/search/omni"
var url: NSURL = NSURL(string: urlPath)!
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
connection.start()
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
self.data.appendData(data)
}
func connectionDidFinishLoading(connection: NSURLConnection!){
var err: NSError
var jsonResult: NSArray = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSArray
for var i = 0; i<jsonResult.count; ++i{
...
}
}
source to share
I tried this sample code to solve your problem. First of all run this http://api.mtgdb.info/search/omni URL in a web browser and copy the answer, then paste in " http://jsonlint.com ", the answer is valid and I get an array of 8 dictionaries, for example id: 37113, 39932, 83737, 106426, 228247, 288937, 382286, 386302 - 8.
In Objective C, it works perfectly and I get the same result as a web browser. But in Swift it behaves strangely, cannot parse the whole repo, get only half of the dictionary as an array object. Get this most of the answer,
Printing description of jsonResult:
(
{
artist = "Arnie Swekel";
cardSetId = JUD;
cardSetName = Judgment;
colors = (
green,
white
);
convertedManaCost = 7;
description = "Trample\nPhantom Nishoba enters the battlefield with seven +1/+1 counters on it.\nWhenever Phantom Nishoba deals damage, you gain that much life.\nIf damage would be dealt to Phantom Nishoba, prevent that damage. Remove a +1/+1 counter from Phantom Nishoba.";
flavor = "";
formats = (
{
legality = Legal;
name = "Odyssey Block";
},
{
legality = Legal;
name = Legacy;
},
{
legality = Legal;
name = Vintage;
},
{
legality = Legal;
name = Freeform;
},
{
legal
I tried this sample code
class ViewController: UIViewController, NSURLConnectionDelegate {
var data:NSMutableData!
var arrvehicls:NSMutableArray!
override func viewDidLoad() {
super.viewDidLoad()
self.data = NSMutableData()
self.arrvehicls = NSMutableArray()
self.startConnection()
}
func startConnection(){
let urlPath: String = "http://api.mtgdb.info/search/omni"
var url: NSURL = NSURL(string: urlPath)!
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
connection.start()
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
self.data.appendData(data)
}
func connectionDidFinishLoading(connection: NSURLConnection!) {
var err: NSError
var jsonResult:NSArray = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSArray
for var i = 0; i<jsonResult.count; ++i {
var dictResult = jsonResult.objectAtIndex(i) as! NSDictionary
var vehicleInfo = Vehicle()
vehicleInfo.id = dictResult.valueForKey("id") as! Int
vehicleInfo.artist = dictResult.valueForKey("artist") as! String
vehicleInfo.cardID = dictResult.valueForKey("cardSetId") as! String
vehicleInfo.cardName = dictResult.valueForKey("cardSetName") as! String
vehicleInfo.colors = dictResult.valueForKey("colors") as! NSArray
vehicleInfo.details = dictResult.valueForKey("description") as! String
vehicleInfo.flavour = dictResult.valueForKey("flavor") as! String
vehicleInfo.formats = NSMutableArray()
var arr = dictResult.valueForKey("formats") as! NSArray
for var j = 0; j<arr.count; ++i {
var dictFormats = arr.objectAtIndex(i) as! NSDictionary
var formats = Formats()
formats.legality = dictFormats.valueForKey("legality") as! String
formats.name = dictFormats.valueForKey("name") as! String
vehicleInfo.formats.addObject(formats)
}
self.arrvehicls.addObject(vehicleInfo)
}
}
}
source to share