Strange printed JSON format - json

I've got a problem getting back a JSON from an HTTP request.
Below my code:
class RestApiManager: NSObject {
static let sharedInstance = RestApiManager()
let baseURL = "url/code.php"
func getParcheggi() {
let request = NSMutableURLRequest(URL: NSURL(string: link1)!)
request.HTTPMethod = "POST"
let postString = ""//id=13&name=Jack
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
print(json) //*** HERE I PRINT THE JSON ***
}catch{
}
}
task.resume()
}}
This is the JSON i'm getting back:
{
"qk_response_sa" = {
ErroreResult = {
Codice = 0;
Messaggio = ok;
};
Siti = (
{
Descrizione = "Quick Morelli";
IDSito = 1;
Indirizzo = "Via Morelli 64";
Localita = Napoli;
MaxAbb = 5;
MaxPre = 5;
Provincia = Na;
},
{
Descrizione = "Quick Linate ";
IDSito = 2;
Indirizzo = "Viale Corsica angolo via Terenzio 6";
Localita = Milano;
MaxAbb = 0;
MaxPre = 300;
Provincia = MI;
},
{
Descrizione = "Quick Stazione Centrale ";
IDSito = 4;
Indirizzo = "Piazza Nazionale snc";
Localita = Napoli;
MaxAbb = 0;
MaxPre = 150;
Provincia = Na;
},
{
Descrizione = "Quick Buenos Aires ";
IDSito = 5;
Indirizzo = "Corso Buenos Aires,21";
Localita = Milano;
MaxAbb = 0;
MaxPre = 50;
Provincia = MI;
},
{
Descrizione = "Quick Lodi ";
IDSito = 6;
Indirizzo = "Via Giovanni Balilla Magistri ";
Localita = Milano;
MaxAbb = 0;
MaxPre = 150;
Provincia = MI;
},
{
Descrizione = "Quick Sempione ";
IDSito = 7;
Indirizzo = "Via Corleone,7 ";
Localita = Milano;
MaxAbb = 0;
MaxPre = 150;
Provincia = MI;
},
{
Descrizione = "Quick Palmanova";
IDSito = 8;
Indirizzo = "Via Benadir, 1";
Localita = Milano;
MaxAbb = 0;
MaxPre = 150;
Provincia = MI;
},
{
Descrizione = "Quick Porta Capuana";
IDSito = 9;
Indirizzo = "Piazza San Francesco";
Localita = Napoli;
MaxAbb = 0;
MaxPre = 80;
Provincia = Na;
},
{
Descrizione = "Quick San Paolo";
IDSito = 11;
Indirizzo = "Via Cinthia";
Localita = Napoli;
MaxAbb = 0;
MaxPre = 0;
Provincia = Na;
},
{
Descrizione = "Quick Porta di Massa";
IDSito = 12;
Indirizzo = "Vico degli scoppettieri 47";
Localita = Napoli;
MaxAbb = 0;
MaxPre = 0;
Provincia = NA;
},
{
Descrizione = "Quick Policlinico";
IDSito = 13;
Indirizzo = "Via Zaccherini Alvisi 12";
Localita = Bologna;
MaxAbb = 0;
MaxPre = 100;
Provincia = Bo;
},
{
Descrizione = "Quick Washington";
IDSito = 14;
Indirizzo = "Via Digione, 5";
Localita = Milano;
MaxAbb = 0;
MaxPre = 100;
Provincia = Mi;
},
{
Descrizione = "Quick Mondadori";
IDSito = 15;
Indirizzo = "Largo di Porta Pradella";
Localita = Mantova;
MaxAbb = 0;
MaxPre = 100;
Provincia = Mn;
},
{
Descrizione = "Quick Mercato";
IDSito = 16;
Indirizzo = "Piazza Matteotti";
Localita = Caserta;
MaxAbb = 0;
MaxPre = 100;
Provincia = Ce;
},
{
Descrizione = "Quick Garage Centrale";
IDSito = 17;
Indirizzo = "Via dei Fossi 507r";
Localita = Firenze;
MaxAbb = 0;
MaxPre = 50;
Provincia = Fi;
},
{
Descrizione = "Quick Aeroporto Palermo ";
IDSito = 19;
Indirizzo = "Aeroporto Falcone e Borsellino - Cinisi ";
Localita = "Palermo ";
MaxAbb = 0;
MaxPre = 180;
Provincia = Pa;
},
{
Descrizione = "Quick Aeroporto Brindisi";
IDSito = 20;
Indirizzo = "Contrada Baroncino, 11/A";
Localita = Brindisi;
MaxAbb = 0;
MaxPre = 70;
Provincia = BR;
},
{
Descrizione = "Quick Mostra Via Terracina ";
IDSito = 22;
Indirizzo = "Via Terracina, 197 ";
Localita = "Napoli ";
MaxAbb = 0;
MaxPre = 300;
Provincia = Na;
},
{
Descrizione = "Quick San Francesco Bari ";
IDSito = 23;
Indirizzo = "Via trevisani,54 A ";
Localita = "Bari ";
MaxAbb = 0;
MaxPre = 150;
Provincia = Ba;
},
{
Descrizione = "Quick Mostra Viale Kennedy ";
IDSito = 24;
Indirizzo = "Viale Kennedy, 54 ";
Localita = "Napoli ";
MaxAbb = 0;
MaxPre = 50;
Provincia = NA;
},
{
Descrizione = "Quick Ospedale Mazzoni -Ascoli Piceno ";
IDSito = 25;
Indirizzo = "Via Salaria Inferiore ";
Localita = "Ascoli Piceno ";
MaxAbb = 0;
MaxPre = 60;
Provincia = AP;
},
{
Descrizione = "Quick Centro Happio - Roma ";
IDSito = 26;
Indirizzo = "Via Cesare Baronio 21-23";
Localita = "Roma ";
MaxAbb = 0;
MaxPre = 100;
Provincia = RM;
},
{
Descrizione = "Quick Bonola - Milano ";
IDSito = 27;
Indirizzo = "Via Quarenghi n\U00b0 23 ";
Localita = "Milano ";
MaxAbb = 0;
MaxPre = 150;
Provincia = MI;
},
{
Descrizione = "Quick Aeroporto Verona Sky Parking ";
IDSito = 28;
Indirizzo = "Via Monte Baldo, 14 - Dossobuono di Villafranca ";
Localita = "Verona ";
MaxAbb = 0;
MaxPre = 100;
Provincia = VR;
},
{
Descrizione = "Quick Yes Parking Aeroporto Milano Malpensa";
IDSito = 29;
Indirizzo = "Via Giusti n\U00b0101 - Somma Lombardo (VA)";
Localita = "Milano ";
MaxAbb = 0;
MaxPre = 600;
Provincia = MI;
},
{
Descrizione = "Sede Napoli";
IDSito = 30;
Indirizzo = "Via ";
Localita = Napoli;
MaxAbb = 0;
MaxPre = 0;
Provincia = NA;
},
{
Descrizione = "Sede Milano";
IDSito = 31;
Indirizzo = "";
Localita = Milano;
MaxAbb = 0;
MaxPre = 0;
Provincia = MI;
}
);
};}
Why I got this type of JSON instead of a standard one:
{"employees":[{"firstName":"John", "lastName":"Doe"},{"firstName":"Anna", "lastName":"Smith"},{"firstName":"Peter", "lastName"enter code here:"Jones"}]}
If I try online with a JSON reader it is shown in the correct way...

Because you are not printing the JSON String, you are printing the result of
try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
which is of AnyObject type.
If you want to see your JSON as text, make a String with the data:
if let stringJSON = String(data: data!, encoding: NSUTF8StringEncoding) {
print(stringJSON)
}

NSJSONSerialization.JSONObjectWithData returns an object with all elements mapped as:
JSON Array -> NSArray
JSON Object -> NSDictionary
JSON Number -> NSNumber (containing number)
JSON String -> NSString
JSON Boolean -> NSNumber (containing boolean)
JSON Null -> NSNull
If you want to use the content retrieved from the URL, you need to convert the result data into some object which you can manipulate with Swift, so NSJSONSerialization.JSONObjectWithData have just done it for you.
You can use the output with casting json to some appropriate type, in your case, dictionary.
See what you can get with this code added after your print(json).
let jsonDict = json as! [String: AnyObject]
print(jsonDict["qk_response_sa"])

Related

How to handle Alamofire responses and reuse in different Views

I am newbie in Xcode, sorry if is a basic question need to use the first response in static view and the second in tableview, maybe is some common way to handle both and read data from other view, I can make both responses return the same keys, or if is better push the first in "hits".
Thanks in advance.
I make the requests with Alamofire with .responseJSON
One returning simple row with the following response:
success({
adultos = 2;
aeropuerto = "<null>";
bebes = 0;
compania = "<null>";
destino = Aeropuerto;
dia = 0;
direccion = "calle aragon";
enapto = 0;
entaxi = 0;
exito = 1;
fecha = "0000-00-00";
finalizado = 0;
})
Other with variable number of rows returning
success({
hits = (
{
destino = "Son Servera";
dia = 24;
direccion = "Torre Marina, Ronda de Sol Ixent, 22";
entaxi = 1;
fecha = "2021-06-24";
hora = "22:20";
"id_reservas" = 958687;
mes = "Thursday June 2021";
"nombre_cliente" = "SERVICIO DE PRUEBA";
origen = Aeropuerto;
},
{
destino = "AIDA Cruceros";
dia = 25;
direccion = prueba;
entaxi = 1;
fecha = "2021-06-25";
hora = "12:00";
"id_reservas" = 959442;
mes = "Friday June 2021";
"nombre_cliente" = "TONI CARDONA";
origen = Aeropuerto;
},
);
})

Unable to parse the json in swift

I have a json response as follows
{
"payment_methods" = (
{
code = checkmo;
title = "Check / Money order";
},
{
code = "paypal_express";
title = "PayPal Express Checkout";
},
{
code = banktransfer;
title = "Bank Transfer Payment";
}
);
totals = {
"base_currency_code" = USD;
"base_discount_amount" = "-258.38";
"base_grand_total" = "2725.37";
"base_shipping_amount" = 400;
"base_shipping_discount_amount" = 0;
"base_shipping_incl_tax" = 400;
"base_shipping_tax_amount" = 0;
"base_subtotal" = "2583.75";
"base_subtotal_with_discount" = "2325.37";
"base_tax_amount" = 0;
"coupon_code" = ADAMAS10;
"discount_amount" = "-258.38";
"grand_total" = "2725.37";
items = (
{
"base_discount_amount" = "134.2";
"base_price" = 1342;
"base_price_incl_tax" = 1342;
"base_row_total" = 1342;
"base_row_total_incl_tax" = 1342;
"base_tax_amount" = 0;
"discount_amount" = "134.2";
"discount_percent" = 10;
"item_id" = 3292;
name = BWBCA14KWGVC015;
options = "[{\"value\":\"18K White Gold\",\"label\":\"Metal\"},{\"value\":\"2\",\"label\":\"size\"}]";
price = 1342;
"price_incl_tax" = 1342;
qty = 1;
"row_total" = 1342;
"row_total_incl_tax" = 1342;
"row_total_with_discount" = 0;
"tax_amount" = 0;
"tax_percent" = 0;
"weee_tax_applied" = "[]";
"weee_tax_applied_amount" = 0;
},
{
"base_discount_amount" = "124.18";
"base_price" = "1241.75";
"base_price_incl_tax" = "1241.75";
"base_row_total" = "1241.75";
"base_row_total_incl_tax" = "1241.75";
"base_tax_amount" = 0;
"discount_amount" = "124.18";
"discount_percent" = 10;
"item_id" = 3342;
name = BWBCA14KWGCV008;
options = "[{\"value\":\"18K White Gold\",\"label\":\"Metal Type\"},{\"value\":\"1.75\",\"label\":\"Ring Size\"}]";
price = "1241.75";
"price_incl_tax" = "1241.75";
qty = 1;
"row_total" = "1241.75";
"row_total_incl_tax" = "1241.75";
"row_total_with_discount" = 0;
"tax_amount" = 0;
"tax_percent" = 0;
"weee_tax_applied" = "[]";
"weee_tax_applied_amount" = 0;
}
);
"items_qty" = 2;
"quote_currency_code" = USD;
"shipping_amount" = 400;
"shipping_discount_amount" = 0;
"shipping_incl_tax" = 400;
"shipping_tax_amount" = 0;
subtotal = "2583.75";
"subtotal_incl_tax" = "2583.75";
"subtotal_with_discount" = "2325.37";
"tax_amount" = 0;
"total_segments" = (
{
code = subtotal;
title = Subtotal;
value = "2583.75";
},
{
code = discount;
title = "Discount (ADAMAS10)";
value = "-258.38";
},
{
code = shipping;
title = "Shipping & Handling (Flat Shipping Charge - Flat Shipping Charge)";
value = 400;
},
{
area = taxes;
code = tax;
"extension_attributes" = {
"tax_grandtotal_details" = (
);
};
title = Tax;
value = 0;
},
{
area = footer;
code = "grand_total";
title = "Grand Total";
value = "2725.37";
}
);
"weee_tax_applied_amount" = "<null>";
};
}
I need to fetch values from total segments.The code is shipping and value is Shipping & Handling (Flat Shipping Charge - Flat Shipping Charge.
I tried to parse the json using the below code
if let res = json as? [String:Any]
{
if let dict = res as? [String:Any]
{
if let total = dict as? [[String:Any]]
{
}
}
}
I am getting value till dict.But i am getting nil in the total variable.
How to get the total segments value?
you're missing out to mention the inner key path of the dictionary. hope this is what you're trying to do
if let res = json as? [String:Any] {
if let paymentMethods = res["payment_methods"] as? [[String:Any]] {
}
if let totals = res["totals"] as? [String:Any] {
if let items = totals["items"] as? [[String:Any]] {
}
}
}
Use this to get the total_segments and value of all code keys:
var codes = [String]()
if let res = json as? [String:Any] {
if let totals = res["totals"] as? [String:Any] {
if let totalSegments = totals["total_segments"] as? [[String:Any]] {
for segment in totalSegments {
if let code = segment["code"] as? String {
codes.append(code)
}
}
}
}
}

parsing the nested json in swift

I am new to swift. Here is a deserialized JSON
Optional({
data = (
{
comments = (
{
comments = good;
"created_date" = "2018-01-01";
id = 2;
likes = 0;
"news_id" = 4;
status = 1;
"user_id" = 5;
}
);
date = 1510167600000;
id = 4;
images = "0c980d3f9cd0393a46bb04995d16177a.jpg";
likes = 25;
title = facebook;
totalcoments = 1;
},
{
comments = (
{
comments = "Good News";
"created_date" = "2018-01-01";
id = 1;
likes = 2;
"news_id" = 7;
status = 1;
"user_id" = 5;
},
{
comments = "well good";
"created_date" = "2018-01-02";
id = 3;
likes = 0;
"news_id" = 7;
status = 1;
"user_id" = 5;
},
{
comments = "well good";
"created_date" = "2018-01-02";
id = 4;
likes = 0;
"news_id" = 7;
status = 1;
"user_id" = 5;
},
{
comments = "well good";
"created_date" = "2018-01-03";
id = 5;
likes = 0;
"news_id" = 7;
status = 1;
"user_id" = 5;
}
);
date = 1511377200000;
id = 7;
images = "0c980d3f9cd0393a46bb04995d16177a.jpg";
likes = 3;
title = don;
totalcoments = 4;
},
{
comments = (
);
date = 1510513200000;
id = 8;
images = "0c980d3f9cd0393a46bb04995d16177a.jpg";
likes = 90;
title = Dell;
totalcoments = 0;
},
{
comments = (
);
date = 1515351600000;
id = 14;
images = "0c980d3f9cd0393a46bb04995d16177a.jpg";
likes = 0;
title = dfsfdsfdfs;
totalcoments = 0;
}
);
message = "Success.";
newsurl = "http://dev.nsol.sg/projects/sneakers/assets/brand_images/thumb/";
success = 1;
totalnews = 4;
})
Here in following class i am trying to parse the json
import UIKit
class getData: NSObject {
var descriptionn : String = ""
var image : String = ""
var likes : String = ""
var comments : Int = 0
var com:String=""
var like:String = ""
func getDataForTableView(results: [[String:Any]], index : Int){
var productArray = [String:Any]()
productArray = results[index]
descriptionn = productArray["title"]! as! String
image = productArray["images"]! as! String
likes=productArray["likes"]! as! String
comments=productArray["totalcoments"]! as! Int
/*{
comments = (
{
comments = good;
"created_date" = "2018-01-01";
id = 2;
likes = 0;
"news_id" = 4;
status = 1;
"user_id" = 5;
}
);*/
//code for parsing the "comments" section of json is
let comment=productArray["comments"] as! [String:Any]?
com = comment!["comments"] as! String
like = comment!["likes"] as! String
}
}
The code for populating the table view cell is below
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "discoveryNewscell") as! DiscoveryNewsTableViewCell
classObject.getDataForTableView(results: results, index: indexPath.row)
cell.sneakerImageView.image=filteredsneakernews[indexPath.row].image
cell.newsTitle.text = classObject.descriptionn
cell.likes.text=classObject.likes
cell.comments.text=String(classObject.comments)
let imageURLPathString = newsurl + classObject.image
print("comments")
print(classObject.image)
let url1 = URL(string: imageURLPathString)
print("xyz", url1)
let data = try? Data(contentsOf: url1!)
if let imageData = data {
let image = UIImage(data: imageData)
cell.sneakerImageView.image = image
}
return cell
}
Here is didSelectRowAt Index path function
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storybaord=UIStoryboard(name: "Main", bundle: nil)
let tabBar=storybaord.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
let DVC = tabBar.viewControllers?[0] as! NewsViewController
classObject.getDataForTableView(results: results, index: indexPath.row)
let imageURLPathString = newsurl + classObject.image
let url1 = URL(string: imageURLPathString)
print("xyz", url1)
let data = try? Data(contentsOf: url1!)
if let imageData = data {
let image = UIImage(data: imageData)
DVC.getImage = image!
}
let news = classObject.descriptionn
DVC.getNews = news
DVC.getnumberOfComment=classObject.comments
print("comment")
//code for passing "comment" part of json to view controller
print(classObject.like)
print(classObject.com)
DVC.getlikes=classObject.like
DVC.getcomment=classObject.com
self.navigationController?.pushViewController(tabBar, animated: true)
}
I want when a particular row is selected from table view it should display the number of "likes" and "comments" that are from nested part of JSON on "Likes Screen" view controller
comments = (
{
comments = good;
"created_date" = "2018-01-01";
id = 2;
likes = 0;
"news_id" = 4;
status = 1;
"user_id" = 5;
}
);
How to read and parse that nested "Comments" in JSON? You can download the project from this link .https://drive.google.com/file/d/1VYM4nywaBYDP2BcyBYPPNUYAu_18mhd9/view?usp=sharing

Populate UITableView from JSON Array using Swift

Dear stackoverflow community,
I have the following function written in swift:
func jsonParsing()
{
let url2 = NSURL(string: "http://localhost:8000/straightred/jsonfixture")
let data = NSData(contentsOfURL: url2!)
var arr = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSArray
for var i = 0 ; i < (arr as NSArray).count ; i++
{
arrDict.addObject((arr as NSArray) .objectAtIndex(i))
}
println(arrDict);
}
Which produces the following output:
(
{
fields = {
"away_team" = Burnley;
awayteamscore = 2;
fixturedate = "2014-11-22T15:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Stoke;
hometeamscore = 1;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136932;
},
{
fields = {
"away_team" = Swans;
awayteamscore = 1;
fixturedate = "2014-11-22T15:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = ManCity;
hometeamscore = 2;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136930;
},
{
fields = {
"away_team" = Sunderland;
awayteamscore = 0;
fixturedate = "2014-11-22T15:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Foxes;
hometeamscore = 0;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 137852;
},
{
fields = {
"away_team" = "West Ham";
awayteamscore = 1;
fixturedate = "2014-11-22T15:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Everton;
hometeamscore = 2;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136929;
},
{
fields = {
"away_team" = "West Bromwich";
awayteamscore = 0;
fixturedate = "2014-11-22T15:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Chelsea;
hometeamscore = 2;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136928;
},
{
fields = {
"away_team" = QPR;
awayteamscore = 0;
fixturedate = "2014-11-22T15:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Newcastle;
hometeamscore = 1;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136931;
},
{
fields = {
"away_team" = ManU;
awayteamscore = 2;
fixturedate = "2014-11-22T17:30:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Arsenal;
hometeamscore = 1;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136927;
},
{
fields = {
"away_team" = Liverpool;
awayteamscore = 1;
fixturedate = "2014-11-23T13:30:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Crystal;
hometeamscore = 3;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136926;
},
{
fields = {
"away_team" = Spurs;
awayteamscore = 2;
fixturedate = "2014-11-23T16:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Hull;
hometeamscore = 1;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136925;
},
{
fields = {
"away_team" = Southampton;
awayteamscore = 1;
fixturedate = "2014-11-24T20:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = "Aston Villa";
hometeamscore = 1;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136924;
}
)
I then want to populate a UITableView with this information. I have the following two functions to achieve this:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrDict.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as TblCell
var homeTeamName : NSString=arrDict[indexPath.row] .valueForKey("home_team") as NSString
var awayTeamName : NSString=arrDict[indexPath.row] .valueForKey("away_team") as NSString
cell.lblHomeTeam.text = homeTeamName
cell.lblAwayTeam.text = awayTeamName
return cell
}
However, the output window shows:
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
It highlights the "var homeTeamName : NSString=arrDict[indexPath.row] .valueForKey("home_team") as NSString" in green and states "EXC_BAD_INSTRUCTION".
I am convinced that I am trying to access the array incorrectly but I am not sure the correct way forward. Any help on this would be appreciated.
Many thanks, ALAN.
Look at your data: "home_team" and "away_team" are values from a level 2 dictionary, in the "fields" key of a level 1 dictionary.
But you try to access "home_team" and "away_team" directly inside your "arrDict" array of level 1 dictionaries.
Array => Dictionaries => Fields key of each dic => your data (in a dictionary)
So you should first access "fields":
let fields = arrDict[indexPath.row].valueForKey("fields") as NSDictionary
Then access the team:
let homeTeamName = fields.valueForKey("home_team") as NSString

Trouble with nested Json data

I'm having a ton of trouble trying to access the nested json data (pasted at the bottom). I'm able write:
var dataResults = jsonResult["data"] as NSDictionary
In order to create a dictionary containing the data within "data", however, Xcode will not allow me to call on anything within "data" such as the information within "current_condition". I've tried to make current_condition it's own dictionary like so:
var results = dataResults["current_condition"] as NSDictionary
But it would seem that this is turning up as nil
I have also attempted accessing using the standard method for calling nested loops:
var dataResults = jsonResult["data"]["current_condition"] as NSDictionary
But this results in a compiler error.
Any help? Much appreciated!
Json data:
{
data = {
"current_condition" = (
{
cloudcover = 0;
humidity = 68;
"observation_time" = "01:39 AM";
precipMM = "0.0";
pressure = 1017;
"temp_C" = 20;
"temp_F" = 68;
visibility = 10;
weatherCode = 143;
weatherDesc = (
{
value = Mist;
}
);
weatherIconUrl = (
{
value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0006_mist.png";
}
);
winddir16Point = NE;
winddirDegree = 50;
windspeedKmph = 7;
windspeedMiles = 4;
}
);
request = (
{
query = "London, United Kingdom";
type = City;
}
);
weather = (
{
date = "2014-07-25";
precipMM = "1.5";
tempMaxC = 27;
tempMaxF = 81;
tempMinC = 14;
tempMinF = 57;
weatherCode = 353;
weatherDesc = (
{
value = "Light rain shower";
}
);
weatherIconUrl = (
{
value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png";
}
);
winddir16Point = NE;
winddirDegree = 54;
winddirection = NE;
windspeedKmph = 15;
windspeedMiles = 10;
},
{
date = "2014-07-26";
precipMM = "5.8";
tempMaxC = 28;
tempMaxF = 83;
tempMinC = 16;
tempMinF = 61;
weatherCode = 176;
weatherDesc = (
{
value = "Patchy rain nearby";
}
);
weatherIconUrl = (
{
value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png";
}
);
winddir16Point = NNE;
winddirDegree = 12;
winddirection = NNE;
windspeedKmph = 11;
windspeedMiles = 7;
},
{
date = "2014-07-27";
precipMM = "0.2";
tempMaxC = 26;
tempMaxF = 80;
tempMinC = 13;
tempMinF = 55;
weatherCode = 116;
weatherDesc = (
{
value = "Partly Cloudy";
}
);
weatherIconUrl = (
{
value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png";
}
);
winddir16Point = NW;
winddirDegree = 321;
winddirection = NW;
windspeedKmph = 14;
windspeedMiles = 9;
},
{
date = "2014-07-28";
precipMM = "1.9";
tempMaxC = 26;
tempMaxF = 78;
tempMinC = 12;
tempMinF = 54;
weatherCode = 116;
weatherDesc = (
{
value = "Partly Cloudy";
}
);
weatherIconUrl = (
{
value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png";
}
);
winddir16Point = N;
winddirDegree = 351;
winddirection = N;
windspeedKmph = 13;
windspeedMiles = 8;
},
{
date = "2014-07-29";
precipMM = "0.0";
tempMaxC = 28;
tempMaxF = 82;
tempMinC = 16;
tempMinF = 60;
weatherCode = 113;
weatherDesc = (
{
value = Sunny;
}
);
weatherIconUrl = (
{
value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png";
}
);
winddir16Point = NNW;
winddirDegree = 329;
winddirection = NNW;
windspeedKmph = 13;
windspeedMiles = 8;
}
);
};
}
Oddly enough, I have a sample weather app that I converted to Swift. I get my data elsewhere, but I found this library to be a great way to deal with JSON in Swift: https://github.com/dankogai/swift-json/. Much cleaner and easier.
It's because the subscript in Dictionary returns an optional:
subscript (key: KeyType) -> ValueType?
Meaning you have to unwrap the optional before you can downcast:
let dataResults = jsonResult["data"]! as NSDictionary
let results = dataResults["current_condition"]! as NSDictionary
As of Beta 3 you can access the value directly without downcasting, like this:
let dataResults = jsonResult["data"]!
let results = dataResults["current_condition"]
However I would suggest you wrap it around an if-let to make sure you don't unwrap nil values.