I am trying to print a list of currencies and their symbols from a JSON file that I have locally in the project
guard let path: String = Bundle.main.path(forResource: "Common-Currency", ofType: "json") else {return}
let url = URL(fileURLWithPath: path)
do {
let jsonData = try Data(contentsOf: url)
let json = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
print(json)
guard let jsonArray = json as? [Any] else { return }
for currency in jsonArray {
guard let eachCurrency = currency as? [String: Any] else {return}
guard let currencyName = eachCurrency["code"] else {return}
guard let currencySymbol = eachCurrency["symbol_native"] else {return}
print(currencyName)
print(currencySymbol)
}
}
catch {
print(error)
}
This is the current code I have, however when I run it only the print(json) command gets executed, not the other 2 prints. What am I doing wrong?
The json looks something like this:
{
"Currencies" : {
"USD": {
"symbol": "$",
"name": "US Dollar",
"symbol_native": "US$",
"decimal_digits": 2,
"rounding": 0,
"code": "USD",
"name_plural": "US dollars"
},
"CAD": {
"symbol": "CA$",
"name": "Canadian Dollar",
"symbol_native": "CA$",
"decimal_digits": 2,
"rounding": 0,
"code": "CAD",
"name_plural": "Canadian dollars"
},
"EUR": {
"symbol": "€",
"name": "Euro",
"symbol_native": "€",
"decimal_digits": 2,
"rounding": 0,
"code": "EUR",
"name_plural": "euros"
},
"AED": {
"symbol": "AED",
"name": "United Arab Emirates Dirham",
"symbol_native": "د.إ.",
"decimal_digits": 2,
"rounding": 0,
"code": "AED",
"name_plural": "UAE dirhams"
},
"AFN": {
"symbol": "Af",
"name": "Afghan Afghani",
"symbol_native": "؋",
"decimal_digits": 0,
"rounding": 0,
"code": "AFN",
"name_plural": "Afghan Afghanis"
},
The root object is a dictionary [String:Any]. The currencies are the value for key currencies which is also a dictionary.
You get the currency information with
let url = Bundle.main.url(forResource: "Common-Currency", withExtension: "json")!
do {
let jsonData = try Data(contentsOf: url)
let json = try JSONSerialization.jsonObject(with: jsonData) as! [String:Any]
print(json)
let currencies = json["Currencies"] as! [String: [String:Any]]
for (key, currency) in currencies {
let currencyName = currency["name"] as! String
let currencySymbol = currency["symbol_native"] as! String
print(key) // == `code`
print(currencyName)
print(currencySymbol)
}
}
catch {
print(error)
}
For an alphabetical order you have to get the keys, sort them and get the currency dictionary by key.
let currencies = json["Currencies"] as! [String: [String:Any]]
let currencyCodes = currencies.keys.sorted()
for code in currencyCodes {
let currency = currencies[code]!
let currencyName = currency["name"] as! String
let currencySymbol = currency["symbol_native"] as! String
print(code)
print(currencyName)
print(currencySymbol)
}
Related
let voicemailFiles = voicemail.value( forKey: "voicemail") as! [AnyObject]
// Could not cast value of type '__NSDictionaryI' (0x10ca8a228) to 'NSArray' (0x10ca89d78)
let voicemailFiles = voicemail.value( forKey: "voicemail") as! [String : String]
// Could not cast value of type '__NSArrayI' (0x104df9448) to 'NSDictionary' (0x104df7fa8).
When trying to put the JSON ( as seen below) into a variable, i am hitting the above errors for the 2 methods of casting for "voicemail".
let config = voicemail.value(forKey: "config") as AnyObject
This line for the key of config works perfectly.
The variable of voicemail is an [AnyObject] value of the key, voicemailboxes
{
"voicemailboxes": [
{
"config": {
"id": "5",
"description": "Test"
},
"voicemail": [
{
"id": "id001",
"caller": "...",
"caller_UK": "...",
"called": "+...",
"called_UK": "...",
"received": "...",
"duration_seconds": "..."
},
{
"id": "id002",
"caller": "...",
"caller_UK": "...",
"called": "..."
}
]
}
]
}
voicemailboxes is an array of Dictionary.
voicemail is also an array of Dictionary.
Need to parse them appropriately.
On playground:
let json = """
{
"voicemailboxes": [
{
"config": {
"id": "5",
"description": "Test"
},
"voicemail": [
{
"id": "id001",
"caller": "...",
"caller_UK": "...",
"called": "+...",
"called_UK": "...",
"received": "...",
"duration_seconds": "..."
},
{
"id": "id002",
"caller": "...",
"caller_UK": "...",
"called": "..."
}
]
}
]
}
"""
let data = json.data(using: .utf8)!
let jsonDict = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! [String:Any]
let voicemailboxes = jsonDict["voicemailboxes"] as! [Any]
let voicemailboxesfirst = voicemailboxes.first as! [String:Any]
let voicemail = voicemailboxesfirst["voicemail"] as! [Any]
let voicemailfirst = voicemail.first as! [String:Any]
print(voicemailfirst)
Output:
["received": ..., "called": +..., "id": id001, "caller_UK": ..., "duration_seconds": ..., "caller": ..., "called_UK": ...]
As per your JSON it is clear that
voicemailboxes is [[string:Any]] (Array of Dictionary) and
voicemail is [[String:String]] (Array of Dictionary)
NOTE: guard or if let required to check correct response below is just example
You can access it like
((dictMain["voicemailboxes"] as! [[String:Any]]).first!["voicemail"] as! [[String:Any]])
Hope it is helpful to you
use below code
if let any = voicemail.value( forKey: "voicemail") {
if let tmpArray = any as? Array {
let voicemailFiles = tmpArray
}
}
I am having the following web service response in which I need parse the parameters based on "MainServiceCategories_ID"
Here is the response
{
"Entity": {
"ID": 20021,
"UserTypeID": 1,
"UserType": null,
"UserID": 30046,
"Code": "lPx1lTEq",
"Name": "بدراء",
"EnglishName": "Badra",
"IconProfileImageID": "",
"MainImageProfileImageID": "EED372C3-5C4F-E711-8100-0CC47A343427",
"LocationLng": 78.37021,
"LocationLat": 17.432563,
"Services": [
{
"ID": 11788,
"MainServiceCategories_ID": 3,
"ServiceCategories_ID": 6,
"Children": [
{
"Parent_ID": 6,
"Name": “John”,
"EnglishName": “Johnny”,
},
{
"Parent_ID": 6,
"Name": “Ronny”,
"EnglishName": “Ronny”,
}]
},
{
"ID": 11788,
"MainServiceCategories_ID": 2,
"ServiceCategories_ID": 6,
"Children": [
{
"Parent_ID": 6,
"Name": “Samuel”,
"EnglishName": “Samuel”,
},
{
"Parent_ID": 6,
"Name": “Badri”,
"EnglishName": “Badri”,
}]
},
{
"ID": 11788,
"MainServiceCategories_ID": 3,
"ServiceCategories_ID": 6,
"Children": [
{
"Parent_ID": 6,
"Name": “emma”,
"EnglishName": “Emma”,
},
{
"Parent_ID": 6,
"Name": “Sean”,
"EnglishName": “Sean”,
}]
}]
this is the code I am writing to do it
do{
let entityDic = responseDictionary["Entity"] as? [String: Any]
let servicesDic = entityDic?["Services"] as? [Any]
for i in 0 ..< servicesDic!.count {
let services = servicesDic?[i] as? [String: Any]
let availableServices = services!["EnglishName"]
servicesNamesArr.append(availableServices! as! String)
let children = services!["Children"]
childrenServices.append(children!)
let MainServiceCategories_ID = services!["MainServiceCategories_ID"]
MainServiceCategoriesID.append(MainServiceCategories_ID!)
}
print("childrenServices \(childrenServices)")
}
catch let error{
print(error)
}
I am able to get upto children dictionaries. Now the thing is I am trying to parse "English name" under children section and separate them based on "MainServiceCategories_ID" where I am getting fatal error in unwrapping the value.
for example if MainServiceCategories_ID =1 then that "English Name" need to be store in an array and if MainServiceCategories_ID =2 then into another array
The problem is, you are trying to parse "EnglishName" as a key of an element of the "Services" array and not as a key of a "Children" element. "Services" does not have a key "EnglishName", so you can't store that in servicesNamesArr. servicesDic should also be an Array of Dictionaries and not an array of Any objects.
See below code using guard statements using safe optional unwrapping:
do{
guard let entityDic = responseDictionary["Entity"] as? [String: Any] else {return}
guard let servicesDic = entityDic?["Services"] as? [[String:Any]] else {return}
for service in services {
guard let MainServiceCategoryID = service["MainServiceCategories_ID"] as? Int else {return}
MainServiceCategoriesID.append(MainServiceCategoryID)
guard let children = service["Children"] as? [[String:Any]] else {return}
childrenServices.append(children)
if MainServiceCategoryID == 3 {
for child in children {
if let availableService = child["EnglishName"] as? String {
servicesNamesArr.append(availableService)
}
}
}
}
print("childrenServices \(childrenServices)")
} catch let error{
print(error)
}
I have a NodeJS API that queries a MongoDB cluster and returns nearby data in the following format. The issue is I can't enumerate the locations with Swift 3 and XCode 8.1.
[
{
"_id": "57b03fa32c6835946afd358c",
"location": {
"coordinates": [
144.2,
-34
],
"elevation": "1",
"type": "Point"
},
"name": "Resource 01",
"url": "http://www.url.com/Resource01.html"
},
{
"_id": "34b03fa32c6835946afd358c",
"location": {
"coordinates": [
154.2,
-35.3
],
"elevation": "1",
"type": "Point"
},
"name": "Resource 02",
"url": "http://www.url.com/Resource02.html"
}
]
This is my Query code.
let json = try! JSONSerialization.jsonObject(with: data)
print(json)
if let statusesArray = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String: AnyObject]],
let jsonbranch = statusesArray?[0] as? [String: AnyObject],
let therecord = jsonbranch["_id"] as? String {
print ("_id: " + therecord)
}
I tried to use SwiftyJSON but it does not work either?
Solved:
if let jsonBranch = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String: AnyObject]],
let name = jsonBranch?[0]["name"] as? String {
print ("name: " + name)
}
if let jsonBranch = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String: AnyObject]],
let name = jsonBranch?[1]["name"] as? String {
print ("name: " + name)
}
I am having issues traversing a JSON retrieved from the Google Books API.
I am able to traverse and print the "id" from "items" but how do I get further down the json to print the "title" from "volumeInfo"?
Any tips or pointers appreciated.
JSON from Google:
{
"kind": "books#volumes",
"totalItems": 555,
"items": [
{
"kind": "books#volume",
"id": "BZXn-3QtQ_UC",
"etag": "Phnt2wzOFMo",
"selfLink": "https://www.googleapis.com/books/v1/volumes/BZXn-3QtQ_UC",
"volumeInfo": {
"title": "Revisiting Stephen King",
"subtitle": "A Critical Companion",
"authors": [
"Sharon A. Russell"
],
Swift Code
let url = NSURL(string: "https://www.googleapis.com/books/v1/volumes?q=stephen+king")
NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in
if error != nil {
print(error)
return
}
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers)
if json is [String: AnyObject] {
let items = json["items"] as? [[String: AnyObject]]
for item in items! {
let kind = item["id"] as? String
print(kind)
}
}
} catch let jsonError {
print(jsonError)
}
}.resume()
}
volumeInfo is Dictionary so you need to cast it like [String: AnyObject], and then get the title from that volumInfo Dictionary.
for item in items! {
let kind = item["id"] as? String
print(kind)
if let volumeInfo = item["volumeInfo"] as? [String: AnyObject] {
print(volumeInfo["title"])
}
}
i currently trying to decode Json in Xcode, but i not succed to get one of them.
This is what i get :
[
{
"id": "1",
"title": "bmw",
"price": "500.00",
"description": "330",
"addedDate": "2015-05-18 00:00:00",
"user_id": "1",
"user_name": "CANOVAS",
"user_zipCode": "32767",
"category_id": "1",
"category_label": "VEHICULES",
"subcategory_id": "2",
"subcategory_label": "Motos",
"bdd": {}
}
"pictures":
[
{ "name": "http://cars.axlegeeks.com/sites/default/files/4315/media/images/2014_BMW_Z4_sDrive28i_3790993.jpg"
}
]
}
]
And i want to get the value of "name" for the "Pictures" lines but i have the error "unexpectedly found nil while unwrapping value".
For the others values i proceed this way :
let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as! NSDictionary
//Browse into JSON to get datas
let resp = jsonData["0"] as! NSDictionary
let user_nameR = resp["user_name"] as! String
let user_zipCodeR = resp["user_zipCode"] as! String
let titleR = resp["title"] as! String
let priceR = resp["price"] as! String
let descriptionR = resp["description"] as! String
Thank's for your help !
Pictures is in not in the subdictionary your other values are. This should work, but you should check all values if they are nil before you force cast them.
if let pictureArray = jsonData["pictures"] as? NSArray
{
if let pictureDict = pictureArray.firstObject as? NSDictionary
{
if let pictureName = pictureDict.objectForKey("name") as? String
{
NSLog("Picture name: %#", pictureName)
}
}
}
jsonData contains two sub-dictionaries, one without a key and one with key 'pictures'. Pictures is an array containing one sub-dictionary.