In Swift, check if a key exists in JSON - json

I have the following function. It is supposed to take the current song playing information from the API webpage. When a song is playing, the page says:
{ "artist" : "Artist", "song" : "Song" }
When no data is available (No song is playing):
{ "error" : "no song metadata provided" }
Is there a way to check if the "error" key exists? Or what can I do with this issue?
func songNowPlaying() {
let endpoint = NSURL(string: "http://api.vicradio.org/songs/current")
let jsonData = NSData(contentsOfURL: endpoint!)
do {
let parsed = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers) as! NSMutableArray
SongNowText.text = parsed.valueForKey("song").componentsJoinedByString("")
ArtistNowText.text = parsed.valueForKey("artist").componentsJoinedByString("")
}
catch let error as NSError {
print("\(error)")
}
}

The URL returns a JSON dictionary not an array. You can check for both cases this way
func songNowPlaying() {
let endpoint = NSURL(string: "http://api.vicradio.org/songs/current")
if let jsonData = NSData(contentsOfURL: endpoint!) {
do {
let parsed = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers) as! [String:String]
if let error = parsed["error"] {
print("Result error: \(error)")
} else {
if let song = parsed["song"] {
SongNowText.text = song
}
if let artist = parsed["artist"] {
ArtistNowText.text = artist
}
}
}
catch let error as NSError {
print("Serialization error: \(error)")
}
}
}
as the JSON object can be downcasted to [String:String] any further downcasting is not needed.

Related

Saving Json data in Realm in swift

I want to save json data in Realm in swift . But I am getting error like:
Terminating app due to uncaught exception 'RLMException', reason: 'Realm accessed from incorrect thread'"
In this situation what to use - "GCD" or "operation"
I am adding my code here
func getDataFromServer(){
let personData = Person()
let headers = [
"user_id": "1",
"access_token": "5ae39568b47d3edf12345dc7ccddf519",
]
let request = NSMutableURLRequest(url: NSURL(string: "http://prvy.in/sme/assgnment_ios/api/user/data")! as URL)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
} else {
if let data = data {
do{
guard let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary else{
return
}
if let resultArray = json["data"] as? NSArray{
for allData in resultArray {
if let resultData = allData as? NSDictionary{
let regdId = resultData[REGD_ID] as? String
if let firstName = resultData[FIRST_NAME] as? String{
print(firstName)
personData.firstName = firstName
}
self.saveReamData(personData)
}
}
}catch{
print("error loading data")
}
}
}
})
dataTask.resume()
}
My save function is:
func saveReamData(_ person: Person) {
let backgroundQueue = DispatchQueue(label: ".realm", qos: .background)
backgroundQueue.async {
do {
try self.realm?.write {
self.realm?.add(person)
}
} catch {
print("error saving to realm")
}
}
}
If you created Realm instance in the main queue you can use it only on the main queue. In your code you are trying to write data to Realm in the background queue but you have created Realm instance in the main queue.
If you want to write data to Realm in the background queue use this code from Realm documentation:
DispatchQueue(label: "background").async {
autoreleasepool {
let realm = try! Realm()
guard let person = realm.resolve(personRef) else {
return // person was deleted
}
try! realm.write {
person.name = "Jane Doe"
}
}
}

setValuesForKeys resulting to this error: setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key type

This is the first time that I will be attempting to use the method setValuesForKeys and for some reason, I keep stumbling upon the error as stated below:
setValue:forUndefinedKey:]: this class is not key value
coding-compliant for the key type.
I've gone through several related questions here but to no avail. Here's my code below:
class ResumeCategory: NSObject {
var name: String?
var resumes: [Resume]?
static func getJSON() {
let urlString = "https://api.letsbuildthatapp.com/appstore/featured"
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { (data, _, error) in
DispatchQueue.main.async {
if let error = error {
print("Failed to get data from URL: ", error)
return
}
guard let data = data else { return }
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary
print(json["categories"]!)
var resumeCategories = [ResumeCategory]()
for dict in json["categories"] as! [[String: Any]] {
let resumeCategory = ResumeCategory()
resumeCategory.setValuesForKeys(dict)
resumeCategories.append(resumeCategory)
}
} catch let error {
print("Failed to parse server response: ", error)
}
}
}.resume()
}

Finishing the completion handler that fetches JSON from web, SWIFT [duplicate]

I am currently trying to download, parse and print JSON from an URL.
So far I got to this point:
1) A class (JSONImport.swift), which handles my import:
var data = NSMutableData();
let url = NSURL(string:"http://headers.jsontest.com");
var session = NSURLSession.sharedSession();
var jsonError:NSError?;
var response : NSURLResponse?;
func startConnection(){
let task:NSURLSessionDataTask = session.dataTaskWithURL(url!, completionHandler:apiHandler)
task.resume();
self.apiHandler(data,response: response,error: jsonError);
}
func apiHandler(data:NSData?, response:NSURLResponse?, error:NSError?)
{
do{
let jsonData : NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary;
print(jsonData);
}
catch{
print("API error: \(error)");
}
}
My problem is, that the data in
do{
let jsonData : NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary;
print(jsonData);
}
remains empty.
When I debug,the connection starts successfully, with the given url as a parameter. But my jsonData variable doesn't get printed. Instead the catch block throws the error, stating that there is no data in my variable:
API error: Error Domain=NSCocoaErrorDomain Code=3840 "No value."
Can someone please help me with this?
What am I missing?
Thank you all very much in advance!
[Edited after switching from NSURL Connection to NSURLSession]
Here's an example on how to use NSURLSession with a very convenient "completion handler".
This function contains the network call and has the "completion handler" (a callback for when the data will be available):
func getDataFrom(urlString: String, completion: (data: NSData)->()) {
if let url = NSURL(string: urlString) {
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url) { (data, response, error) in
// print(response)
if let data = data {
completion(data: data)
} else {
print(error?.localizedDescription)
}
}
task.resume()
} else {
// URL is invalid
}
}
You can use it like this, inside a new function, with a "trailing closure":
func apiManager() {
getDataFrom("http://headers.jsontest.com") { (data) in
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: [])
if let jsonDict = json as? NSDictionary {
print(jsonDict)
} else {
// JSON data wasn't a dictionary
}
}
catch let error as NSError {
print("API error: \(error.debugDescription)")
}
}
}

how to read data from json in swift2

I am trying to read email from a json file in swift(2.2) which is:
{ "employees" : [
{
"name": "sudhanshu",
"email": "sudhanshu.bharti#digitalavenues.com",
"password": "password"
"profilePic": ""
},
{
"name": "prokriti",
"email": "prokriti.roy#digitalavenues.com",
"password": "password#123",
"profilePic": ""
}
]}
But i am getting error " Error Domain=NSCocoaErrorDomain Code=3840 "Unescaped control character around character 128." UserInfo={NSDebugDescription=Unescaped control character around character 128.}" i have seen earlier posts but unable to find where exactly problem is??
if let path = NSBundle.mainBundle().pathForResource("Employees", ofType: "json") {
if let data = NSData(contentsOfFile: path) {
do {
let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
if let error = jsonResult["error"] {
print("Error is: \(error)")
} else {
if let person = jsonResult["email"] {
print(person) // dictionary[#"quotables"]
}
}
} catch let error as NSError {
print("Error is: \(error)")
}
}
}
Thanks in advance!
"password": "password”
should be
"password": "password"
You have an invalid ” character instead of a ".
Update
Now that you've fixed your invalid character, you can access your data. But you're trying to cast as an NSDictionary something that's actually an array, if I believe the JSON excerpt you showed us.
So you should do something like this instead in your do:
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [[String: String]] {
for jsonDictionary in jsonResult {
if let person = jsonDictionary["email"] {
print(person)
}
}
}
Update and fix
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: AnyObject] {
if let employees = jsonResult["Employees"] as? [[String:String]] {
for employee in employees {
if let person = employee["email"] {
print(person)
}
}
}
}
You are trying to access directly email key from the dictionary. while you need to first access array from the key "employees" & then you need to get value from "email" key.
if let path = NSBundle.mainBundle().pathForResource("Employees", ofType: "json") {
if let data = NSData(contentsOfFile: path) {
do {
let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
if let error = jsonResult["error"] {
print("Error is: \(error)")
} else {
let person = jsonResult["employees"] as! NSArray
for i in 0..<person.count
{
let dict = person.objectAtIndex(i) as! NSDictionary
let strEmail = dict["email"] as! String
print(strEmail)
}
}
} catch let error as NSError {
print("Error is: \(error)")
}
}

How do I get values from a complex JSON object?

Is it possible that someone could show me how to get the names of these pizza places printing out? My application prints out the expected "Status Code: 200". However, my console only shows empty brackets []. I suspect that I am not pulling values from my JSON object properly.
I'm using this link for my API.
Link For API
Question
How can I properly fetch values from my serialized JSON object?
relevant code:
// Response
if let httpResponse = response as? NSHTTPURLResponse where httpResponse.statusCode == 200, let data = data {
print("Status Code: \(httpResponse.statusCode)")
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers)
if let pizzaPlaces = json["response"] as? [[String: AnyObject]] {
for place in pizzaPlaces {
if let name = place ["name"] as? String {
self.PizzaClass.append(name)
}
}
}
} catch {
print("Error Serializing JSON Data: \(error)")
}
print(self.PizzaClass)
}
}).resume()
You need to cast your NSJSONSerialization.JSONObjectWithData result as a [String:AnyObject].
let jsonObject = try NSJSONSerialization.JSONObjectWithData(returnedData, options: .MutableLeaves) as! [String: AnyObject]
Once you have that all you need to do is pay attention to what you're casting. Take the code below for an example. If we want to get our response object using jsonObject["response"] what kind of data structure do we have?
"response": {
"venues": [{
//... continues
}]
}
On the left we have "response" which is a string, on the right we have {} which is an AnyObject. So we have [String: AnyObject]. You just need to think about what object your dealing with piece by piece. Below is a working example that you can just paste into your application.
full working code:
func getJson() {
let request = NSMutableURLRequest(URL: NSURL(string: "https://api.foursquare.com/v2/venues/search?client_id=0F5M0EYOOFYLBXUOKTFKL5JBRZQHAQF4HEM1AG5FDX5ABRME&client_secret=FCEG5DWOASDDYII4U3AAO4DQL2O3TCN3NRZBKK01GFMVB21G&v=20130815%20&ll=29.5961,-104.2243&query=burritos")!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTaskWithRequest(request) { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
guard let testResponse = response as? NSHTTPURLResponse else {
print("\(response)")
return
}
guard let status = HTTPStatusCodes(rawValue: testResponse.statusCode) else {
print("failed to unwrap status")
return
}
print(status)
switch status {
case .Created:
print("ehem")
case .BadRequest:
print("bad request")
case .Ok:
print("ok")
guard let returnedData = data else {
print("no data was returned")
break
}
do {
let jsonObject = try NSJSONSerialization.JSONObjectWithData(returnedData, options: .MutableLeaves) as! [String: AnyObject]
guard let response = jsonObject["response"] as? [String: AnyObject] else { return }
guard let venues = response["venues"] as? [AnyObject] else { return }
guard let location = venues[0]["location"] as? [String:AnyObject] else { return }
guard let formattedAddress = location["formattedAddress"] else { return }
print("response: \n\n \(response)\n------")
print("venues : \n\n \(venues)\n-------")
print("location : \n\n \(location)\n------")
print("formatted address : \n \(formattedAddress)")
} catch let error {
print(error)
}
// update user interface
dispatch_sync(dispatch_get_main_queue()) {
print("update your interface on the main thread")
}
}
}
task.resume()
}
place this either in its own file our outside of the class declaration,
enum HTTPStatusCodes : Int {
case Created = 202
case Ok = 200
case BadRequest = 404
}
Not that this was what you are looking for, but since you are new to Swift take a look at Alamofire. It handles JSON serialization for you. And when you need to chain calls PromiseKit is super slick.
Alamofire.request(.GET, url).responseJSON {response in
switch (response.result) {
case .Success(let value):
let pizzas = JSON(value).arrayValue
for place in pizzaPlaces {
if let name = place ["name"] as? String {
self.PizzaClass.append(name)
}
}
case .Failure(let error):
if let data = response.data, let dataString = String(data: data, encoding: NSUTF8StringEncoding) {
print("ERROR data: \(dataString)")
}
print("ERROR: \(error)")
}
}