Alamofire json swift 2.2 - json

I'm trying to take out some data of facebook place's names with Alamofire but i have no luck.
This is what i've tried and i cant figure out how to make it work.
xcode 7.3
swift2.2
So the result of the graph api is this
{
"data": [
{
"category": "Local business",
"category_list": [
{
"id": "272705352802676",
"name": "Outdoors"
},
{
"id": "115725465228008",
"name": "Region"
}
],
"location": {
"street": "Athens",
"city": "Palai\u00f3n F\u00e1liron",
"state": "",
"country": "Greece",
"zip": "17562",
"latitude": 37.9284637008,
"longitude": 23.6944070162
},
"name": "THE NAME OF THE PLACE",
"id": "THE ID"
}
and i want to go inside "data" and take only the name and the ID of the place which are the 2 last lines.
And this is my code!
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(.GET, "https://graph.facebook.com/search", parameters: ["q": "", "type": "place", "center": "37.928319,23.7036673", "distance": "10000", "access_token": "ACCESS-TOKEN", "expires_in": "5184000"])
.responseJSON { response in
if let JSON = response.result.value {
print("JSON: \(JSON["data"]!["name"]!)")
}
}
}
and i get this error
fatal error: unexpectedly found nil while unwrapping an Optional value
Any idea why?

Ok this was easy i guess and it didn't worth the question but this is the solution
Alamofire.request(.GET, "https://graph.facebook.com/search", parameters: ["q": "", "type": "place", "center": "37.928319,23.7036673", "distance": "10000", "access_token": "475827182628380|6U-mk-1etGQHwrXRSMF4Ht2zOyc", "expires_in": "5184000"])
.responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar["data"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
}
if self.arrRes.count > 0 {
self.kati.reloadData()
}
}
}
And at the cell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("checkInCell", forIndexPath: indexPath)
var dict = arrRes[indexPath.row]
cell.textLabel?.text = dict["name"] as? String
//cell.detailTextLabel?.text = dict["email"] as? String
return cell
}
Just dont forget to use SwiftyJSON too!

Related

How to parse local JSON data in Swift?

How to parse local JSON data where nested (optional) property is same as main.
Items data may be available or may not be available.
struct Category: Identifiable, Codable {
let id: Int
let name: String
let image: String
var items: [Category]?
}
I am using common Bundle extension to parse JSON data.
extension Bundle {
func decode<T: Codable>(_ file: String) -> T {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Failed to locate \(file) in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(file) from bundle.")
}
let decoder = JSONDecoder()
let formatter = DateFormatter()
formatter.dateFormat = "y-MM-dd"
decoder.dateDecodingStrategy = .formatted(formatter)
guard let loaded = try? decoder.decode(T.self, from: data) else {
fatalError("Failed to decode \(file) from bundle.")
}
return loaded
}
}
For eg data :
[
{
"id": 1,
"name": "Apple",
"image": "img_url",
"items" : [
{
"id": 1,
"name": "iPhone",
"image": "img_url",
"items" : [
{
"id": 1,
"name": "iPhone 11 Pro",
"image": "img_url"
},
{
"id": 2,
"name": "iPhone 11 Pro Max",
"image": "img_url"
}
]
},
{
"id": 2,
"name": "iPad",
"image": "img_url",
"items" : [
{
"id": 1,
"name": "iPad mini",
"image": "img_url"
},
{
"id": 2,
"name": "iPad Air",
"image": "img_url"
},
{
"id": 3,
"name": "iPad Pro",
"image": "img_url"
}
]
}
]
},
{
"id": 2,
"name": "Samsung",
"image": "img_url",
"items" : [
{
"id": 1,
"name": "Phone",
"image": "img_url"
},
{
"id": 2,
"name": "Tablet",
"image": "img_url"
}
]
}
]
Nesting is not the issue here, You are facing an Array of Contents. so you should pass [Content] to the decoder like:
let jsonDecoder = JSONDecoder()
try! jsonDecoder.decode([Category].self, from: json)
🎁 Property Wrapper
You can implement a simple property wrapper for loading and decoding all of your properties:
#propertyWrapper struct BundleFile<DataType: Decodable> {
let name: String
let type: String = "json"
let fileManager: FileManager = .default
let bundle: Bundle = .main
let decoder = JSONDecoder()
var wrappedValue: DataType {
guard let path = bundle.path(forResource: name, ofType: type) else { fatalError("Resource not found") }
guard let data = fileManager.contents(atPath: path) else { fatalError("File not loaded") }
return try! decoder.decode(DataType.self, from: data)
}
}
Now you can have any property that should be loaded from a file in a Bundle like:
#BundleFile(name: "MyFile")
var contents: [Content]
Note that since the property should be loaded from the bundle, I raised a FatalError. Because the only person should be responsible for these errors is the developer at the code time (not the run time).

Encodable JSON dictionary in Swift 4

With the help of #matt. I was able to solve it and the finished code is below for anyone who will encounter similer issues in the near future.
Given the below JSON
{
"restaurants": [
{
"id": 1,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 3,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 4,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 5,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 6,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
}
]
}
And I am trying to access the arrays within the Dictionary with this code into a table. So I have made a Struct. Below is the content of my Structfile.
struct Response: Codable {
let restaurants: [Restaurant]
struct Restaurant: Codable {
let id: Int
let restaurantName: String
let restaurantPicture: String
let status: String
let workingDays: String
let workingHours: String
}
}
This is my main View Controller
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tabelView: UITableView!
var restaurants = [Response]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
downloadJSON {
self.tabelView.reloadData()
}
tabelView.delegate = self
tabelView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restaurants.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = restaurants[indexPath.row].restaurantName.capitalized
return cell
}
func downloadJSON(completed: #escaping ()->()){
let url = URL(string: "http://localhost:8888/tableview/tableView.php")
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error == nil{
do{
let downloadedRestaurants = try JSONDecoder().decode(Response.self, from: data!)
print(downloadedRestaurants.restaurants.map{$0.restaurantName})
self.restaurant.append(downloadedRestaurants)
DispatchQueue.main.async {
completed()
}
}catch{
print(error)
}
}
}.resume()
}
}
Thanks #matt. I hope this piece becomes useful to other newbies or anyone who is having similar issues
Cheers!
The problem is that you are trying to decode as [RestaurantStats]. But look at your JSON! It is not an array of RestaurantStats. It is not an array at all. It is a dictionary with a single key, "restaurants". You need to create that struct and decode as that.
EDIT Since I gave my answer, you have revised your code to declare an outer struct Response. And that's correct — and is exactly what I said you should do. So decode as Response and you're all set.

Repeated values in UITable cells from api json

After I do http call from api I get json file then I loop through it to get specific values:
if let value: AnyObject = response.result.value as AnyObject? {
let json = JSON(value)
for (key, subJson) in json {
let drivers = subJson["driver"]
for (key, subJson) in drivers {
let status = subJson["status"].stringValue
let date = subJson["created_at"].stringValue
let name = subJson["name"].stringValue
let pivot = subJson["pivot"]
for (key, subJson) in subJson["pivot"] {
let type = pivot["type"].stringValue
let data = Driverj(name: name, status: status, created_at: date)
self.data.append(data)
}
DispatchQueue.main.async {
self.tableview.reloadData()
}
}
}
}
in a separate file I connect the values with objects:
class DriverC: UITableViewCell {
#IBOutlet weak var status : UILabel!
#IBOutlet weak var date : UILabel!
#IBOutlet weak var job : UILabel!
#IBOutlet weak var update : UILabel!
}
And in another file I have the data saved :
import Foundation
import ObjectMapper
class Driverj : Mappable {
var status : String?
var name : String?
var created_at : String?
required init?(map: Map) {
}
required init(name: String , status : String , created_at : String ) {
self.status = status
self.name = name
self.created_at = created_at
}
func mapping(map: Map) {
status <- map["status"]
name <- map["name"]
created_at <- map["created_at"]
}
}
And finally here is how it's displayed
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DriverCell", for: indexPath) as! DriverC
let entry = data[indexPath.row]
cell.date.text = entry.date
cell.job.text = entry.job
cell.status.text = entry.status
cell.update.text = entry.name
return cell
}
My question is when I run my project I get three cells with the same data repeated. Why ?
the json files :
[
{
"id": 4,
"name": null,
"email": "5#5.com",
"status": 0,
"confirmed": false,
"street": "street ",
"niehgborhood": "North",
"city": "Sf",
"national_id": "1009090",
"phone": "9000",
"size_of_house": null,
"created_at": "2016-12-04 13:55:52",
"updated_at": "2017-03-08 14:03:44",
"deleted_at": null,
"driver": [
{
"name": "unknown",
"age": "25",
"Smoker": "No",
"language": "English",
"religion": "Muslim",
"created_at": null,
"updated_at": "2017-03-08 13:48:55",
"status": "تم",
"pic": "http://localhost:8000/images/1488714520.jpg",
"id": 1,
"pivot": {
"user_id": 4,
"driver_id": 1,
"type": "driver"
}
},
{
"name": "Jae",
"age": "30",
"Smoker": "No",
"language": "English",
"religion": "Muslim ",
"created_at": "2017-02-28 09:36:15",
"updated_at": "2017-03-08 08:46:06",
"status": "ok",
"pic": "http://localhost:8000/images/1488714520.jpg",
"id": 2,
"pivot": {
"user_id": 4,
"driver_id": 2,
"type": "driver"

Swift3 Json Parsing - how to access relevant fields

Having real difficulties accessing the info required in Swift 3 from a JSON, here's what I have:
override func viewDidLoad()
{
super.viewDidLoad()
let url = URL(string: "http://api.brewerydb.com/v2/beers?key=e3bdce7d0a80584c784cdc4b02459add&name=budweiser")
URLSession.shared.dataTask(with:url!) { (data, response, error) in
if error != nil {
print(error!)
}
else {
do {
let parsedData = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
// print(parsedData)
let dataLevel = parsedData["data"] as? NSDictionary
print(dataLevel)
let abv = dataLevel?["abv"] as? AnyObject
print(abv!)
} catch let error as NSError {
print(error)
}
}
}.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
the link to the JSON file is in the code - why is my code not returning the 'data' section, and how could I retrieve the beer 'name', 'abv' and 'description'?
When you deal with JSON I think it is helpful to use tools like:
jsonformatter.curiousconcept.com
or
jsonlint.com
which help me a lot understanding the structure and what kind of data I am dealing with.
If you look at your json in you can notice that, as Eric Aya said, data is an array not a dictionary:
{
"currentPage": 1,
"numberOfPages": 1,
"totalResults": 1,
"data": [{
"id": "1P45iR",
"name": "Budweiser",
"nameDisplay": "Budweiser",
"description": "Known as \u201cThe King of Beers\u201d, Budweiser was first introduced by Adolphus Busch in 1876 and is brewed with the same high quality standards today. Budweiser is a medium-bodied, flavorful, crisp American-style lager, craft brewed with a blend of premium hop varieties, and associated with the core American values of celebration and optimism.",
"abv": "5",
"glasswareId": 5,
"srmId": 5,
"availableId": 1,
"styleId": 93,
"isOrganic": "N",
"labels": {
"icon": "https:\/\/s3.amazonaws.com\/brewerydbapi\/beer\/1P45iR\/upload_Y13vwL-icon.png",
"medium": "https:\/\/s3.amazonaws.com\/brewerydbapi\/beer\/1P45iR\/upload_Y13vwL-medium.png",
"large": "https:\/\/s3.amazonaws.com\/brewerydbapi\/beer\/1P45iR\/upload_Y13vwL-large.png"
},
"status": "verified",
"statusDisplay": "Verified",
"servingTemperature": "cold",
"servingTemperatureDisplay": "Cold - (4-7C\/39-45F)",
"createDate": "2012-01-03 02:42:55",
"updateDate": "2016-03-21 19:54:11",
"glass": {
"id": 5,
"name": "Pint",
"createDate": "2012-01-03 02:41:33"
},
"srm": {
"id": 5,
"name": "5",
"hex": "FBB123"
},
"available": {
"id": 1,
"name": "Year Round",
"description": "Available year round as a staple beer."
},
"style": {
"id": 93,
"categoryId": 8,
"category": {
"id": 8,
"name": "North American Lager",
"createDate": "2012-03-21 20:06:46"
},
"name": "American-Style Lager",
"shortName": "American Lager",
"description": "Light in body and very light to straw in color, American lagers are very clean and crisp and aggressively carbonated. Flavor components should b e subtle and complex, with no one ingredient dominating the others. Malt sweetness is light to mild. Corn, rice, or other grain or sugar adjuncts are often used. Hop bitterness, flavor and aroma are negligible to very light. Light fruity esters are acceptable. Chill haze and diacetyl should be absent.",
"ibuMin": "5",
"ibuMax": "13",
"abvMin": "3.8",
"abvMax": "5",
"srmMin": "2",
"srmMax": "4",
"ogMin": "1.04",
"fgMin": "1.006",
"fgMax": "1.01",
"createDate": "2012-03-21 20:06:46",
"updateDate": "2015-04-07 15:39:26"
}
}],
"status": "success"
}
The following code works but I am sure there is a better way to get abv with less code:
override func viewDidLoad()
{
super.viewDidLoad()
let url = URL(string: "http://api.brewerydb.com/v2/beers?key=e3bdce7d0a80584c784cdc4b02459add&name=budweiser")
URLSession.shared.dataTask(with:url!) { (data, response, error) in
if error != nil {
print(error!)
}
else {
do {
let parsedData = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: Any]
let jsonArrayData = parsedData?["data"] as! NSArray
let data = jsonArrayData[0] as! NSDictionary
let abv = data["abv"]
print(abv)//Optional(5) in your case
} catch let error as NSError {
print(error)
}
}
}.resume()
}

Parse JSON from GoogleApiBooks

I'm trying to parse this JSON but it don't works.
This is the JSON:
{
"kind": "books#volumes",
"totalItems": 1,
"items": [
{
"kind": "books#volume",
"id": "uCHmPQAACAAJ",
"etag": "aBZ3KnoRsq4",
"selfLink": "https://www.googleapis.com/books/v1/volumes/uCHmPQAACAAJ",
"volumeInfo": {
"title": "Psicologia delle folle",
"authors": [
"Gustave Le Bon"
],
"publishedDate": "2004",
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "8850206240"
},
{
"type": "ISBN_13",
"identifier": "9788850206247"
}
],
"readingModes": {
"text": false,
"image": false
},
"pageCount": 251,
"printType": "BOOK",
"categories": [
"Psychology"
],
"maturityRating": "NOT_MATURE",
"allowAnonLogging": false,
"contentVersion": "preview-1.0.0",
"language": "it",
},
"saleInfo": {
"country": "IT",
"saleability": "NOT_FOR_SALE",
"isEbook": false
},
"accessInfo": {
"country": "IT",
"viewability": "NO_PAGES",
"embeddable": false,
"publicDomain": false,
"textToSpeechPermission": "ALLOWED",
"epub": {
"isAvailable": false
},
"pdf": {
"isAvailable": false
},
,
"accessViewStatus": "NONE",
"quoteSharingAllowed": false
}
}
]
}
And this is the code that i'm using:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var url = "https://www.googleapis.com/books/v1/volumes?q=isbn9788850206247&key=my-key"
Alamofire.request(url).responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar["items"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
print(self.arrRes)
}
if self.arrRes.count > 0 {
self.tableProva.reloadData()
}
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableProva.dequeueReusableCell(withIdentifier: "cellProva", for: indexPath) as! CellProvaClass
var dict = arrRes[indexPath.row]
cell.titleLabel?.text = dict["title"] as? String
cell.authorLabel?.text = dict["authors"] as? String
return cell
}
How can I solve this problem?
This is your JSON Reponse ( in short format) :
And this is the data structure (Array and dictionary format) :
Now you have to access : title and authors so here is your value in cellforRowAtIndexPath Method , a dictionary having name dict stores values of volumeInfo ( a Dictionary) :
so you have to use this way to access title :
cell.titleLabel?.text = dict["volumeInfo"]["title"] as? String
//To Fetch Author array
if let authorArray = dict["volumeInfo"]["authors"] as? NSArray{
print(authorArray)
}
and then to access author is an array so which index of value you want to show in cell.authorLabel .
In Second Screenshot those which are enclosed with {} is Dictionary and [] is Array
First try to show Title and then same way try to fetch value of author array and set according to your requirement.
Feel Free to comment if any further issue.
When you are parsing the JSON on line
if let resData = swiftyJsonVar["items"].arrayObject {
you will receive an object of NSDictionary which contains all other nested elements & arrays in keys. In your tableView delegate you are directly accessing the value from arrRes by unboxing it to NSDictionary object. All the entries are available in an array named items which further has a nested object volumeInfo which contains the info about title & another array of Authors.
You need to extract the further nested objects from arrRes object to use it in your tableView cellForRowAtIndex delegate function.
Let me know if you need any further help.