I am new to Swift. I want to fetch some json data from the server using the url. I tried many other solutions but they didn't work. I want to print the duration key (text and value) from the array and then print it in console.
The Json data is attached below
{
"status": "OK",
"rows": [
{
"elements": [
{
"duration": {
"text": "3 hours 49 mins",
"value": 13725
},
"distance": {
"text": "225 mi",
"value": 361715
},
"status": "OK"
}
]
}
],
"origin_addresses": [
"Washington, DC, USA"
],
"destination_addresses": [
"New York, NY, USA"
]
}
Attached Code
func getdatajson1(){
if let url = URL(string: "http://www.json-generator.com/api/json/get/bQywstyfkO?indent=2") {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
let res = try JSONDecoder().decode(Root.self, from: data)
print(res.rows)
} catch let error {
print(error)
}
}
}.resume()
}
}
struct Root: Codable {
let rows: [Root2]
}
struct Root2: Codable {
let elements: [Root3]
}
struct Root3: Codable {
let elements: [node]
}
struct node: Codable {
let duration : [valuesarray]
}
struct valuesarray: Codable {
let text : String
let value : Int
}
The duration is an Object and not an Array, also change your names and you can use this:
struct Root: Decodable {
let rows: [Rows]
}
struct Rows: Decodable {
let elements: [Elements]
}
struct Elements: Decodable {
let duration, distance: LocationValues
}
struct LocationValues: Decodable {
let text: String
let value: Int
}
func getdatajson1(){
if let url = URL(string: "http://www.json-generator.com/api/json/get/bQywstyfkO?indent=2") {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
let res = try JSONDecoder().decode(Root.self, from: data)
if let row = res.rows.first, let elements = row.elements.first {
print(elements.duration.text) //This is how you can get the text value
print(elements.distance.text) //This will print the distance
}
} catch let error {
print(error)
}
}
}.resume()
}
}
Replace your codable struct with the below
class Result: Codable {
var status:String?
var rows:[Row]?
var origin_addresses:[String]?
var destination_addresses:[String]?
}
class Row: Codable {
var elements:[Element]?
}
class Element: Codable {
var status:String?
var duration:Duration?
var distance:Distance?
}
class Duration: Codable {
var text:String?
var value:Int?
}
class Distance: Codable {
var text:String?
var value:Int?
}
You should update your node model like below
struct node: Codable {
let duration : valuesarray
let distance : valuesarray
let status : String
}
And you can access your duration data from API response like below
if let rows = res.rows, rows.count > 0 {
//Access the element objects from rows
let arrElements = rows[0].elements, arrElements.count > 0 {
if let durationData = arrElements[0].duration { //get your duration object
print(durationData.text)
print(durationData.value)
}
}
}
try this:
and a hint: go to https://app.quicktype.io/ -> here you can paste your json and you will get your datastructs for free! ;)
func getdatajson1(){
if let url = URL(string: "http://www.json-generator.com/api/json/get/bQywstyfkO?indent=2") {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
let res = try JSONDecoder().decode(Welcome.self, from: data)
print(res.rows)
} catch let error {
print(error)
}
}
}.resume()
}
}
getdatajson1()
struct Welcome: Codable {
let status: String
let rows: [Row]
let originAddresses, destinationAddresses: [String]
enum CodingKeys: String, CodingKey {
case status, rows
case originAddresses = "origin_addresses"
case destinationAddresses = "destination_addresses"
}
}
// MARK: - Row
struct Row: Codable {
let elements: [Element]
}
// MARK: - Element
struct Element: Codable {
let duration, distance: Distance
let status: String
}
// MARK: - Distance
struct Distance: Codable {
let text: String
let value: Int
}
Related
I'm trying to retrieve maps values from a Firebase Firestore JSON file. So far I've been able to get any field I wanted but I'm having trouble with maps since they are so nested.
Here you can see an image of the JSON.
And here you can see as text.
"user_metrics": {
"arrayValue": {
"values": [
{
"mapValue": {
"fields": {
"point1": {
"integerValue": "0"
},
"point2": {
"integerValue": "0"
},
"metricDescription": {
"stringValue": "Distanza Pupillare"
},
"metricResult": {
"doubleValue": 6.27
},
"metricDescriptionEn": {
"stringValue": "Distance between pupils"
},
"metricTitle": {
"stringValue": "DBP"
}
}
}
},
And I'm decoding it as follows:
struct SessionResponse : Codable {
let sessions : [Session_Struct]
private enum CodingKeys : String, CodingKey {
case sessions = "documents"
}
}
struct SessionStringValue : Codable {
let value : String
private enum CodingKeys : String, CodingKey {
case value = "stringValue"
}
}
struct Session_Struct : Codable {
let is_first_session : Bool
let device : String
let glassesRefs : [String]
let ref_catalogo : String
let total_time : Double
let data_inizio_sessione : String
let data_fine_user_session : String
let device_id : String
let lineaRefs : [String]
let modelRefs : [String]
let user_metrics : [String]
private enum SessionKeys : String, CodingKey {
case fields
}
private enum FieldKeys : String, CodingKey {
case is_first_session
case device
case glassesRefs
case ref_catalogo
case total_time
case data_inizio_sessione
case data_fine_user_session
case device_id
case lineaRefs
case modelRefs
case user_metrics
}
// MARK: - Total Time
struct TotalTime: Codable {
let doubleValue: Double
}
// MARK: - First Session
struct FirstSession: Codable {
let booleanValue: Bool
}
// MARK: - ListaRefsSizes
struct MapMetrics: Codable {
let arrayValue: MetricsArrayValue
}
// MARK: - ArrayValue
struct MetricsArrayValue: Codable {
let values: [ValueMetric]
}
// MARK: - ArrayValue
struct ValueMetric: Codable {
let mapValue : MapValue
}
// MARK: - ListaRefsSizes
struct ListaRefsGlasses: Codable {
let arrayValue: ArrayValue
}
// MARK: - ArrayValue
struct ArrayValue: Codable {
let values: [Value]
}
// MARK: - Value
struct Value: Codable {
let stringValue: String
}
// MARK: - Value
struct MapValue: Codable {
let mapValue: String
}
// MARK: - Value
struct ReferenceValue: Codable {
let referenceValue: String
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: SessionKeys.self)
let fieldContainer = try container.nestedContainer(keyedBy: FieldKeys.self, forKey: .fields)
is_first_session = try fieldContainer.decode(FirstSession.self, forKey: .is_first_session).booleanValue
device = try fieldContainer.decode(SessionStringValue.self, forKey: .device).value
ref_catalogo = try fieldContainer.decode(ReferenceValue.self, forKey: .ref_catalogo).referenceValue
total_time = try fieldContainer.decode(TotalTime.self, forKey: .total_time).doubleValue
data_inizio_sessione = try fieldContainer.decode(SessionStringValue.self, forKey: .data_inizio_sessione).value
data_fine_user_session = try fieldContainer.decode(SessionStringValue.self, forKey: .data_fine_user_session).value
device_id = try fieldContainer.decode(SessionStringValue.self, forKey: .device_id).value
//Lists
glassesRefs = try fieldContainer.decode(ListaRefsGlasses.self, forKey: .glassesRefs)
.arrayValue.values.map{ $0.stringValue }
lineaRefs = try fieldContainer.decode(ListaRefsGlasses.self, forKey: .lineaRefs)
.arrayValue.values.map{ $0.stringValue }
modelRefs = try fieldContainer.decode(ListaRefsGlasses.self, forKey: .modelRefs)
.arrayValue.values.map{ $0.stringValue }
user_metrics = try fieldContainer.decode(MapMetrics.self, forKey: .user_metrics)
.arrayValue.values.map{ $0.mapValue } // <-- How to read this??
}
}
My issue is that I'm not being able to read the array inside the field "user_metrics". Any idea on how to achieve that? Thanks to anyone who can help!
import Foundation
// MARK: - Welcome
struct Welcome: Codable {
let userMetrics: UserMetrics
enum CodingKeys: String, CodingKey {
case userMetrics = "user_metrics"
}
}
// MARK: - UserMetrics
struct UserMetrics: Codable {
let arrayValue: ArrayValue
}
// MARK: - ArrayValue
struct ArrayValue: Codable {
let values: [Value]
}
// MARK: - Value
struct Value: Codable {
let mapValue: MapValue
}
// MARK: - MapValue
struct MapValue: Codable {
let fields: Fields
}
// MARK: - Fields
struct Fields: Codable {
let point1, point2: Point
let metricDescription: Metric
let metricResult: MetricResult
let metricDescriptionEn, metricTitle: Metric
}
// MARK: - Metric
struct Metric: Codable {
let stringValue: String
}
// MARK: - MetricResult
struct MetricResult: Codable {
let doubleValue: Double
}
// MARK: - Point
struct Point: Codable {
let integerValue: String
}
there is structure for your json
call the api request and get data to Welcome(renamed it as you want)
func requestHTTP(urlString:String,completionHandler:#escaping(_ model:Welcome?,_ error:Error?) -> Void) {
guard let requestUrl = URL(string: urlString) else {return}
URLSession.shared.dataTask(with: requestUrl) { (data, httpUrlResponse, error) in
if(error == nil && data != nil && data?.count != 0) {
do {
let response = try JSONDecoder().decode(Welcome.self, from: data!)
completionHandler(response,nil)
} catch {
debugPrint("error")
completionHandler(nil,error)
}
} else {
completionHandler(nil,error)
}
}.resume()
}
and your will get your data easily
func getDataOnController() {
requestHTTP(urlString: "your url here") { model, error in
if let model = model {
let mapVlues = model.userMetrics.arrayValue.values.map({$0.mapValue})
print(mapVlues)
print(mapVlues.count)
}
}
}
currently I'm trying to decode JSON with a nested Array. The nested array can have some random numbers of the object inside it. I try to decode it but turns out it return an errors
CodingKeys(stringValue: "itenaries", intValue: nil),
debugDescription : "Expected to decode Array<Any> but found a dictionary
Sample JSON data
{
"itenaries": {
"days":
[
[
{
"itenary_id":0,
"itenary_location_name":"Batu Caves Temple"
}
],
[
{
"itenary_id":0,
"itenary_location_name":"KL Tower "
},
{
"itenary_id":1,
"itenary_location_name":"KL Forest Eco Park"
}
]
]
}
}
My Struct
struct Itenaries : Codable {
let itenaries : [[Days]]
}
struct Days : Codable {
let itenary_id : Int
let itenary_location_name : String
}
Decoding Implementation
let decoder = JSONDecoder()
let itenary = try decoder.decode(Itenaries.self, from: fileData)
print(itenary.itenaries[0][0].itenary_id)
Where do you decode the days key? That's the problem. You need an intermediate struct
struct Root : Decodable {
let itenaries : Itenary
}
struct Itenary : Decodable {
let days : [[Days]]
}
...
let result = try decoder.decode(Root.self, from: fileData)
print(result.iternaries.days[0][0].itenary_id)
i'd probably do something like
struct Name:Codable {
var itenaries:itenaries
}
struct itenaries:Codable {
var days = [[Days]]
}
struct Days : Codable {
let itenary_id : Int
let itenary_location_name : String
}
so basically according the structure of your Json file
Root struct -> itenaries -> [[days]]
hope you understand :)
[Edited]
you can try these.
I'm getting correct result using this approach
Result
struct MainResponse : Codable {
let itenaries : Itenaries
}
struct Itenaries : Codable {
let days : [[Days]]
}
struct Days : Codable {
let itenary_id : Int
let itenary_location_name : String
}
if let path = Bundle.main.path(forResource: "nested_array", ofType: "json") {
do {
let responseData = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let decoder = JSONDecoder()
let mainResponse = try decoder.decode(MainResponse.self, from: responseData)
print(mainResponse.itenaries.days[0][0].itenary_id)
print(mainResponse.itenaries.days[0][0].itenary_location_name)
print(mainResponse.itenaries.days[1][0].itenary_id)
print(mainResponse.itenaries.days[1][0].itenary_location_name)
print(mainResponse.itenaries.days[1][1].itenary_id)
print(mainResponse.itenaries.days[1][1].itenary_location_name)
// output
// 0
// Batu Caves Temple
// 0
// KL Tower
// 1
// KL Forest Eco Park
} catch let error {
print(error.localizedDescription)
}
}
Your model is not correct, replace it by the following:
struct ItenariesResponse: Codable {
let itenaries: Itenaries
}
struct Itenaries: Codable {
let days: [[Day]]
}
struct Day: Codable {
let itenaryID: Int
let itenaryLocationName: String
enum CodingKeys: String, CodingKey {
case itenaryID = "itenary_id"
case itenaryLocationName = "itenary_location_name"
}
}
Then replace the type you decode like that:
let itenary = try decoder.decode(ItenariesResponse.self, from: fileData)
I am trying to get the value of "price" key which is "25"
I am getting this response Json From Backend
{
"errorCode": 0,
"message": "Request successfully served.",
"data": {
"games": {
"TWELVEBYTWENTYFOUR": {
"jackpot_amount": "KES 40,000.00",
"draw_date": "2021-05-21 10:59:45",
"extra": {
"jackpotAmount": 40000,
"unitCostJson": [
{
"currency": "KES",
"price": 25
}
]
},
}
},
"currentTime": {
"date": "2021-05-20 22:28:18.738038"
}
}
}
This is my code so far :
fetchData { (dict, error) in
let playerLoginInfo = dataDict["data"] as? NSDictionary
let playerGameInfo = playerLoginInfo?.value(forKey: "games") as? NSDictionary
if let TWELVEBYTWENTYFOUR = playerGameInfo?.value(forKey: "TWELVEBYTWENTYFOUR") as? NSDictionary {
let extra = TWELVEBYTWENTYFOUR.value(forKey: "extra") as? NSDictionary
let unitCostJson = extra?.value(forKey: "unitCostJson") as? NSArray
print("price")
print(unitCostJson?.value(forKey: "price") as? Any)
}
}
I get this is console :
Optional(Optional(<__NSSingleObjectArrayI 0x600001f091d0>(
25
)
))
I have seen this question How can I access values within Optional NSSingleObjectArrayI? but I couldn't figure out a solution
Edit:
I have now used Codeable to get data:
struct Resp: Codable {
let errorCode: Int
let message: String
let data: Dat
}
struct Dat: Codable {
let games: Games
let currentTime: CurrentTime
}
struct Games: Codable {
let game_code: String
let datetime: String
let estimated_jackpot: String
let guaranteed_jackpot: String
let jackpot_title: String
let jackpot_amount: String
let draw_date: String
let extra: Extra
let next_draw_date: String
let active: String
}
struct Extra: Codable {
let currentDrawNumber: Int
let currentDrawFreezeDate: String
let currentDrawStopTime: String
let jackpotAmount: Int
let unitCostJson: [UnitCostJson]
}
struct UnitCostJson: Codable {
let currency: String
let price: Int
}
struct CurrentTime: Codable {
let date: String
let timezone_type: Int
let timezone: String
}
I'm trying to get value from price now with this code
do{
let resp:Resp = try JSONDecoder().decode(Resp.self , from:data);
let data = resp.data
let games = data.games
let extra = games.extra
let unitCostJson = extra.unitCostJson
print(unitCostJson[0].price)
}
catch{
GlobalFunctions.shared.callOnMainThread {
self.showAlert(Message: "Something went wrong. Please retry.")
}
}
It is going into catch
How should I get the data inside on the unitCostJson now??
I butchered your struct and removed any irrelevant properties (compared to the json), if you want to add them back then you need to use an CodingKey enum
struct Resp: Codable {
let errorCode: Int
let message: String
let data: Dat
}
struct Dat: Codable {
let games: [String:Games]
let currentTime: CurrentTime
}
struct Games: Codable {
let extra: Extra
}
struct Extra: Codable {
let unitCostJson: [UnitCostJson]
}
struct UnitCostJson: Codable {
let currency: String
let price: Int
}
struct CurrentTime: Codable {
let date: String
}
Now you can access the unitCost like this
let unitCost = resp.data.games["TWELVEBYTWENTYFOUR"]?.extra.unitCostJson
I am trying to parse the data and display on the screen but i am getting " Value of type 'EmployeeData' has no member 'employee_name' "
What i am missing ?
I created my struct, parsed data and tried to divide into two parts. first part will be related with listing, second part is all data.
struct EmployeeData: Codable {
var data: Employee
var status: String
}
struct Employee: Codable {
var employee_name: String
var employee_salary: String
var employee_age: String
}
class WebServices {
func getData(completion: #escaping (EmployeeData?) -> ()){
guard let url = URL(string:"http://dummy.restapiexample.com/api/v1/employees")
else { fatalError("There is error!") }
URLSession.shared.dataTask(with: url) { (data, response,error) in
guard let data = data, error == nil else {
DispatchQueue.main.async{
completion(nil)
}
return
}
let empleyees = try? JSONDecoder().decode(EmployeeData.self, from: data)
DispatchQueue.main.async {
completion(empleyees)
}
}.resume()
}
}
class MVDesingnListView: ObservableObject {
}
struct MVDesignCellView {
let employeeDatas: EmployeeData
init(employeeDatas: EmployeeData) {
self.employeeDatas = employeeDatas
}
var employee_name: String {
self.employeeDatas.employee_name
}
}
The compiler is all right. Your struct EmployeeData has no member employee_name.
You need to go to the employee first, to get her name:
var employee_name: String {
self.employeeDatas.data.employee_name
}
should do the job.
I am having a lot of trouble trying to decode this JSON with Swift 4.
{
"Items": [
{
"id": 1525680450507,
"animal": "bee",
"type": "insect",
"diet": [
"a",
"b",
"c"
]
}
],
"Count": 1,
"ScannedCount": 5
}
Here's where I try to decode
let decoder = JSONDecoder()
let data = try decoder.decode([Animal].self, from: data)
I have created a struct like this
struct Animal: Codable {
var id: Int
var animal: String
var type: String
var diet: [String]
}
let decoder = JSONDecoder()
let data = try decoder.decode(ItemsResponse.self, from: data)
This doesn't work. I get an error that says
"Expected to decode Array<\Any> but found a dictionary instead."
So I thought maybe I needed something like this
struct ItemsResponse: Codable {
var Items: [Animal]
var Count: Int
var ScannedCount: Int
}
But this doesn't work either. Now I get
"Expected to decode Array<\Any> but found a string/data instead."
How do I make a struct that will decode this JSON?
let data = try decoder.decode([Animal].self, from: data)
[Animal].self is not correct you can use it like this :
struct DataJson: Codable {
let items: [Item]
let count, scannedCount: Int
enum CodingKeys: String, CodingKey {
case items = "Items"
case count = "Count"
case scannedCount = "ScannedCount"
}
}
struct Item: Codable {
let id: Int
let animal, type: String
let diet: [String]
}
// MARK: Convenience initializers
extension DataJson {
init(data: Data) throws {
self = try JSONDecoder().decode(DataJson.self, from: data)
}
func jsonData() throws -> Data {
return try JSONEncoder().encode(self)
}
func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
return String(data: try self.jsonData(), encoding: encoding)
}
}
extension Item {
init(data: Data) throws {
self = try JSONDecoder().decode(Item.self, from: data)
}
func jsonData() throws -> Data {
return try JSONEncoder().encode(self)
}
func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
return String(data: try self.jsonData(), encoding: encoding)
}
}
Try this:
import Foundation
let json = """
{
"Items": [
{
"id": 1525680450507,
"animal": "bee",
"type": "insect",
"diet": [
"a",
"b",
"c"
]
}
],
"Count": 1,
"ScannedCount": 5
}
"""
struct Animal: Codable {
var id: Int
var animal: String
var type: String
var diet: [String]
}
struct ItemsResponse: Codable {
var Items: [Animal]
var Count: Int
var ScannedCount: Int
}
let data = try! JSONDecoder().decode(ItemsResponse.self, from: json.data(using: .utf8)!)
Of course, you should properly handle the possible failures (i.e. don't do try!, and don't force-unwrapp the json.data()! part)
But the code above works and hopefully answers your question.