Json Serialisation Swift 3 - ios

I am trying to serialise the json in the code below, the logs print out the display names successfully but I get a crash with an error:
fatal error: unexpectedly found nil while unwrapping an Optional value
on the following lines:
print(item["display-name"]! as!String)
Blockquoteself.tableData.append(item["display-name"] as! String)
I can't seem to figure out why, any help much appreciated!
let url = NSURL(string: "https://www.asmserver.co.uk/sally/parsexml.php")!
let task = URLSession.shared.dataTask(with: url as URL) { (data, response, error) -> Void in
if let urlContent = data {
do {
if let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: []) as? [[String:AnyObject]] {
for item in jsonResult {
print(item["display-name"]! as!String)
self.tableData.append(item["display-name"] as! String)
}
}
} catch {
print("JSON serialization failed")
}
} else {
print("ERROR FOUND HERE")
}
DispatchQueue.main.async(execute: { () -> Void in
self.tableView.reloadData()
})
self.tableView.isUserInteractionEnabled = true
}
task.resume()

You should make sure that you really have a value before you use it and specially before using as!.
Do like this instead:
for item in jsonResult {
guard let name = item["display-name"] as? String else { continue }
print(name)
self.tableData.append(name)
}
If the guard succeeds then you have a value and can use the name variable. You can also add several conditions to the guard statement.

As an alternative to the guard statement, you could also use the similar if let construct:
if let item = item["display-name"] as? String {
print(item)
} else {
print("No display name")
}

Related

API call function with completion handler crashes when accessed from different VC

Can someone fix my function code because I have created a API call function which will get the imageURL for the specific object in my class and display the results in the second view controller. I have created custom completion handler so that the code from second VC is only executed when dowloading of the imageURL is completed.
However, when I am testing this function in the second view controller to print me data that it has arrived I am getting a crash on the print statement line.
Here is the code for my API call function located in Model class file:
func parseImageData(finished: () -> Void) {
let urlPath = _exerciseURL
let url = URL(string: urlPath!)
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print("Error while parsing JSON")
}
else {
do {
if let data = data,
let fetchedImageData = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [String:Any],
let images = fetchedImageData["results"] as? [[String: Any]] {
for eachImage in images {
let imageUrl = eachImage["image"] as! String
self._imageUrl = URL(string: imageUrl)
}
print(self._imageUrl)
}
}
catch {
print("Error while parsing data.")
}
}
}
task.resume()
finished()
}
And here in the second view controller I am just testing if I can access the code block:
override func viewDidLoad() {
super.viewDidLoad()
exercise.parseImageData() {
print("Arrived Here?") // I am getting crash on this line moving to debug navigator.
}
}
If the crash says something about force unwrapping nil then it's probably because let task = URLSession.shared.dataTask(with: url!) is unwrapping url which is a nil optional variable here.
But your completion handler is called in the wrong place anyway, try putting your finished() callback into the do statement instead. Because finished was executed the moment you called exercise.parseImageData()
if error != nil {
print("Error while parsing JSON")
}
else {
do {
if let data = data,
let fetchedImageData = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [String:Any],
let images = fetchedImageData["results"] as? [[String: Any]] {
for eachImage in images {
let imageUrl = eachImage["image"] as! String
self._imageUrl = URL(string: imageUrl)
}
print(self._imageUrl)
finished()
}
}
catch {
print("Error while parsing data.")
}
}

Instagram API Relationship Endpoints followed-by

I am trying to get a list of users that is following me, but it doesn't seems to give me any result. This is the code I am using:
func followers(){
print("1")
let url = URL(string: "https://api.instagram.com/v1/users/self/followed-by?access_token=\(self.currentAccessTolken)")
URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in
guard let data = data, error == nil else { return }
print("2")
do {
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String:Any]
print("3")
if let userJson = json["data"] as? [String : AnyObject] {
print("4")
if let username = userJson["username"] as? String {
print("username: \(username)")
}
if let fullname = userJson["full_name"] as? String {
print("full name: \(fullname)")
}
}
} catch let error as NSError {
print(error)
}
}).resume()
}
API: https://www.instagram.com/developer/endpoints/relationships/#get_users_followed_by
The printn("1"), printn("2") and printn("3") shows up in the event log, but not the printn("4"). Also it doesn't give me any result of either username or full name. The scopes I use is: basic+follower_list+public_content+relationships. Anyone knows what may be wrong here?

How to loop through JSON object and view it as string - Swift

I just started coding in swift and I am at the point that I can get a single value out of the JSON but I can't seem to get all the values out of it by looping trough the array.
so my question is how do I get all the values out and view it as float or string.
here is my code:
let url = URL(string: "http://api.fixer.io/latest")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil
{
print ("ERROR")
}
else
{
if let content = data
{
do
{
//Array
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
//print(myJson)
for items in myJson [AnyObject] {
print(items)
}
//here is the single value part, it looks for the rates then it puts it in label.
if let rates = myJson["rates"] as? NSDictionary{
if let currency = rates["AUD"]{
print(currency);
self.label.text=String(describing: currency)
}
}
}
catch
{
}
}
}
}
task.resume()
Try this code:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.getJson()
}
func getJson(){
let url = URL(string: "http://api.fixer.io/latest")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil
{
print ("ERROR")
}
else
{
if let content = data
{
do
{
//Dic
guard let myJson:[String:Any] = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String:Any] else {return}
//print(myJson)
for items in myJson {
print(items)
}
//here is the single value part, it looks for the rates then it puts it in label.
if let rates = myJson["rates"] as? NSDictionary{
if let currency = rates["AUD"]{
print(currency);
// self.label.text=String(describing: currency)
}
}
}
catch
{
}
}
}
}
task.resume()
}
}
And the result in the console is like below:
The myJson is the dictionary what you want.
I strongly recommend that you use SwiftyJSON to deal with JSON. It's extremely easy to learn and use.
first, you should install SwiftyJSON via CocoaPods (or any other way you like). then you can code it simply like below:
let url = URL(string: "http://api.fixer.io/latest")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil
{
print ("ERROR")
}
else
{
if let content = data
{
// Initialization
let myJson = JSON(data: content)
// Getting a string using a path to the element
self.label.text = myJson["rates"]["AUD"].stringValue
// Loop test
for (key,value):(String, JSON) in myJson["rates"] {
print("key is :\(key), Value:\(value.floatValue)")
}
}
}
}
task.resume()
try this out:
if let currency = rates["AUD"] as? NSDictionary{
for(key,value) in currency {
// the key will be your currency format and value would be your currency value
}
}

how to access array inside json object in swift

Can't access json object which is array inside json object
i want to access data from json object which have array inside array
and that json file is also uploaded
so pls can anyone check and help me how to get "weather.description"
data
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=13ae70c6aefa867c44962edc13f94404")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print("some error occured")
} else {
if let urlContent = data {
do{
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers)
let newValue = jsonResult as! NSDictionary
print(jsonResult)
let name = newValue["name"]
//Here i am getting name as variable value
//this is not working
let description = newValue["weather"]??[0]["description"]
//this is not working
let description = newValue["weather"]!![0]["description"]
print()
}catch {
print("JSON Preocessing failed")
}
}
}
}
task.resume()
}
I have edited your code a bit, and added a few comments. Basiclly, lets check for the types of your response structure, and get the desired value.
let url = URL(string: "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=13ae70c6aefa867c44962edc13f94404")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print("some error occured")
} else {
if let urlContent = data {
do{
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers)
// I would not recommend to use NSDictionary, try using Swift types instead
guard let newValue = jsonResult as? [String: Any] else {
print("invalid format")
return
}
// Check for the weather parameter as an array of dictionaries and than excess the first array's description
if let weather = newValue["weather"] as? [[String: Any]], let description = weather.first?["description"] as? String {
print(description)
}
}catch {
print("JSON Preocessing failed")
}
}
}
}
task.resume()

Error while parsing JSON in swift 2.0

I am trying to download a list of articles and insert it into a table view. However I seem to be having an issue retrieving the JSON file and parsing it.
My code is as follows:
override func viewDidLoad() {
super.viewDidLoad()
self.downloadArticles()
self.tableView.reloadData()
}
func downloadArticles(){
var url: NSURL
url = NSURL(string: "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20feed%20where%20url=%27www.abc.net.au%2Fnews%2Ffeed%2F51120%2Frss.xml%27&format=json")!
print(url)
let task = NSURLSession.sharedSession().dataTaskWithURL(url){
(data, response, error) in
if (error != nil){
print("Error \(error)")
} else{
self.parseArticleJSON(data!)
}
self.syncCompleted = true
self.tableView.reloadData()
}
task.resume()
}
func parseArticleJSON(articleJSON:NSData)
{
do{
let result = try NSJSONSerialization.JSONObjectWithData(articleJSON, options: NSJSONReadingOptions.MutableContainers) as? NSArray
//let jsonData:NSArray = (try NSJSONSerialization.JSONObjectWithData(articleJSON, options:NSJSONReadingOptions.MutableContainers) as? NSArray)!
let newArticlesArray = result as NSArray!
//NSLog("Found \(newArticlesArray.count) new articles!")
for article in (newArticlesArray as NSArray as! [NSDictionary])
{
print (article.objectForKey("title")! as? String)
//let a = Article (t: <#T##String#>, da: <#T##String#>, de: <#T##String#>, i: <#T##NSURL#>)
//articlesArray.addObject(a);
}
}catch {
print("JSON Serialization error")
}
}
In the parseArticleJSON method (I know it is not all completely finished). I get the error at line:
for article in (newArticlesArray as NSArray as! [NSDictionary])
it says:
fatal error: unexpectedly found nil while unwrapping an Optional value
I have tried doing some research here on these forums, but I was unable to find any response that would be of help to me so I was wondering if somebody would be able to help me.
I need to use the native swift JSON methods to do all this.
Thanks in advance!
The JSON is much more nested:
typealias JSONDictionary = Dictionary<String,AnyObject>
func parseArticleJSON(articleJSON:NSData) {
do {
let jsonObject = try NSJSONSerialization.JSONObjectWithData(articleJSON, options: [])
if let jsonResult = jsonObject as? JSONDictionary,
query = jsonResult["query"] as? JSONDictionary,
results = query["results"] as? JSONDictionary,
newArticlesArray = results["item"] as? [JSONDictionary] {
for article in newArticlesArray {
print(article["title"] as! String)
}
}
} catch let error as NSError {
print(error)
}
}
For that deeply nested JSON it's recommended to use a library like SwiftyJSON.
Since the code is only reading the JSON object, the option MutableContainers is not needed at all and in Swift always use native collection types unless you have absolutely no choice.
try this code,
if let jsonObject: AnyObject = NSJSONSerialization.JSONObjectWithData(articleJSON, options: nil, error:&error) {
if let dict = jsonObject as? NSDictionary {
println(dict)
} else {
println("not a dictionary")
}
} else {
println("Could not parse JSON: \(error!)")
}
hope its helpful

Resources