Read GeoJSON data from MongDB in Xcode 8.1 Swift 3? - json

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)
}

Related

Swift: NSDictionary as NSArray

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
}
}

How to parse JSON data from local JSON File swift 3?

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)
}

Parsing Json Files in Swift 3

I would like to parse a local JSON file but I am unaware of how to do so in Swift 3
My Current Code doesn't seem to work
I keep receiving this error:
'jsonObject' produces 'Any', not the expected contextual result type 'NSArray'
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController
{
var allEntries: String!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func LoadAllQuestionsAndAnswers
{
let path = Bundle.main.path(forResource: "content", ofType: "json")
let jsonData : NSData = NSData(contentsOfFile: path!)!
allEntries = JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers, error: nil) as String
NSLog(allEntries)
}
}
I am loading the data from this json file "content.json"
[
{
"id" : "1",
"question": "What is the fastest fish in the ocean?",
"answers": [
"Sailfish",
"Lion Fish",
"Flounder",
"Tiger Shark",
"Swordfish"
],
"difficulty": "1"
},
{
"id" : "2",
"question": "Which animal has the most legs?",
"answers": [
"Millipede",
"Shrimp",
"Octopus",
"Dog",
"Lion"
],
"difficulty": "1"
}
]
let path = Bundle.main.path(forResource: "sample", ofType: "son")
let jsonData = try? NSData(contentsOfFile: path!, options: NSData.ReadingOptions.mappedIfSafe)
print(jsonData!)
let jsonResult: NSDictionary = try! JSONSerialization.jsonObject(with: jsonData as! Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
// save below sample json in a file and name it as sample.json
{
"blogs": [
{
"id": 111,
"url": "http://roadfiresoftware.com/blog/",
"name": "Roadfire Software Blog"
},
{
"id": 345,
"url": "https://developer.apple.com/swift/blog/",
"name": "Swift Developer Blog"
}
]
}
The root object of the JSON is an array
var allEntries = [[String:Any]]()
...
allEntries = try! JSONSerialization.jsonObject(with: jsonData, options: []) as! [[String:Any]]
mutableContainers is not needed at all in Swift

json traverse for Google Books API

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"])
}
}

Unwrapping Json Swift (found nil)

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.