Commit f4ec3857 authored by gabrieso's avatar gabrieso
Browse files

simple example of JSON Parser class for further use in Events for...

simple example of JSON Parser class for further use in Events for Food-Settings and SbbAbos-Settings
parent 1fe884ac
//
// JSONParser.swift
// Amiv
//
// Created by Gabriel Sonderegger on 13.10.18.
// Copyright © 2018 Amiv an der ETH. All rights reserved.
//
import Foundation
import UIKit
public class JSONParser {
public func getJSONData(Filename: String){
guard let path = Bundle.main.path(forResource: Filename, ofType: "json") else {
debugPrint("failed to get JSON Path")
return}
do {
let content = try String(contentsOfFile:path, encoding: String.Encoding.utf8)
let jsonData = content.data(using: .utf8)!
let decoder = JSONDecoder()
let result = try decoder.decode(Root.self, from: jsonData)
dump(result)
} catch {
print("Error info: \(error)")
}
}
}
//Definiere JSON Dictionarys als structs, nach Vorlage SampleEventAdditFields.json
struct Root : Decodable {
private enum CodingKeys : String, CodingKey { case properties = "properties" }
let properties : PropertiesStruct
}
struct PropertiesStruct : Decodable {
private enum CodingKeys : String, CodingKey {
case sbbAbo = "SBB_Abo"
case Food = "Food"
case SFR = "Special Food Requirements"
}
let sbbAbo : sbbAboStruct
let Food : FoodStruct
let SFR : SFRStruct
}
struct sbbAboStruct : Decodable {
private enum CodingKeys : String, CodingKey {
case type = "type"
case Enum = "enum"
}
let type : String
let Enum : [String]
}
struct FoodStruct : Decodable {
private enum CodingKeys : String, CodingKey {
case type = "type"
case Enum = "enum"
}
let type : String
let Enum : [String]
}
struct SFRStruct : Decodable {
private enum CodingKeys : String, CodingKey {
case type = "type"
}
let type : String
}
{
"$schema":"http://json-schema.org/draft-04/schema#",
"additionalProperties":false,
"title":"Additional Fields",
"type":"object",
"properties":
{
"SBB_Abo":
{
"type":"string",
"enum":["None","GA","Halbtax","Gleis 7"]
},
"Food":
{
"type":"string",
"enum":["Omnivor","Vegi","Vegan","Other"]
},
"Special Food Requirements":
{
"type":"string"
}
},
"required":["SBB_Abo","Food"]
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment