-1

im trying to get in Swift the parameters "bicisDisponibles" and "anclajesDisponibles" from this JSON. I have tried many ways to do it but none of them worked.

I have tried this but it doesn't work :(:

Alamofire.request("https://www.zaragoza.es/sede/servicio/urbanismo-infraestructuras/estacion-bicicleta/34.json", method: .get, parameters: ["rf":"html"], encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in          
  switch(response.result) {
    case .success(_):
      if let data = response.result.value{
        print(response.result.value)
      }
      break

    case .failure(_):
       print(response.result.error)
       break
  }
}

I want to get it like a var to put it on an UILabel :)

blwond
  • 1
  • 2
  • 2
    Possible duplicate of [How to parse a JSON file in swift?](https://stackoverflow.com/questions/24013410/how-to-parse-a-json-file-in-swift) – inokey Aug 31 '18 at 09:55
  • If you are only interested in getting ONE value, you might use `JSONSerialization`. If you are interested in getting the whole (or partial) response as a custom object (titles, id, geometry, etc.), use `Codable`. – Larme Aug 31 '18 at 09:58
  • Possible duplicate of [How to parse JSON using swift 4](https://stackoverflow.com/questions/50379653/how-to-parse-json-using-swift-4) – Enea Dume Aug 31 '18 at 10:03

3 Answers3

1
struct data: Codeable {
    let id: Int?
    let about: String?
    let title: String?
    ...
    let bicisDisponibles: Int?
    let anclajesDisponibles: Int?
    ...
}

let decoder = JSONDecoder()
let myData = try! decoder.decode(data.self, for: response.result.value)
print(myData.bicisDisponibles)
print(myData.anclajesDisponibles)

Make a struct which has the same Parameters like your JSON - make it Codeable and then decode it with a JSONDecoder. Hope this helps

ValW
  • 353
  • 1
  • 11
  • I have tried this https://pastebin.com/raw/S7XKtbp6 but it doesn't print anything – blwond Aug 31 '18 at 10:14
  • You have a small error. The correct word should be Codable instead of Codeable. And I think id is a String instead of an Int. Check my answer. Good answer though!! – Miguel Isla Aug 31 '18 at 12:54
  • 1
    Yep saw it but he knew it bc he tried it that way (: – ValW Sep 01 '18 at 11:09
1

I have modified a bit the struct of @ValW, changing id to String. Tested in playground with the json as a String.

import UIKit

struct data: Codable {
    let id: String?
    let about: String?
    let title: String?
    let bicisDisponibles: Int?
    let anclajesDisponibles: Int?
}

var json = "{\"id\":\"34\",\"about\":\"http://www.zaragoza.es/ciudad/viapublica/movilidad/bici/detalle_Bizi?oid=34\",\"title\":\"Plaza Magdalena\",\"estado\":\"OPN\",\"bicisDisponibles\":11,\"anclajesDisponibles\":10,\"geometry\":{\"type\":\"Point\",\"coordinates\":[-0.8733258730100609,41.65210655043524]},\"lastUpdated\":\"2018-08-31T11:15:00Z\",\"description\":\"<ul><li>Estado: Operativa</li><li>Bicis disponibles: 11</li><li>Anclajes disponibles: 10</li></ul><p>Actualizado: 13:15</p>\",\"icon\":\"//www.zaragoza.es/contenidos/iconos/bizi/conbicis.png\"}"

let jsonData = json.data(using: String.Encoding.utf8)
let decoder = JSONDecoder()
let myData = try! decoder.decode(data.self, from: jsonData!)

print(myData.bicisDisponibles!)
print(myData.anclajesDisponibles!)

This code prints, 11 for bicisDisponibles and 10 for anclajesDisponibles.

¡Saludos!

Miguel Isla
  • 1,379
  • 14
  • 25
0

You can parse JSON like this code below.

Alamofire.request("https://www.zaragoza.es/sede/servicio/urbanismo-infraestructuras/estacion-bicicleta/34.json", method: .get, parameters: ["rf":"html"], encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

switch(response.result) {
case .success(_):
    if let data = response.result.value{
        if let result = response.result.value as? [String: Any]{
            if let bicisDisponibles = result["bicisDisponibles"] as? Int{
                print(bicisDisponibles)
            }
            if let anclajesDisponibles = result["anclajesDisponibles"] as? Int{
                print(anclajesDisponibles)
            }
        }
    }
    break

case .failure(_):
    print(response.result.error)
    break

}

}

Azat
  • 490
  • 2
  • 13