London Escorts sunderland escorts 1v1.lol unblocked yohoho 76 https://www.symbaloo.com/mix/yohoho?lang=EN yohoho https://www.symbaloo.com/mix/agariounblockedpvp https://yohoho-io.app/ https://www.symbaloo.com/mix/agariounblockedschool1?lang=EN
0.4 C
New York
Saturday, February 15, 2025

ios – Parsing JSON in Swift Error: Anticipated to decode Array however discovered a dictionary as an alternative


I am making an attempt to decode this JSON known as Menu.json

[
  {
    "day": "Monday",
    "locationName": "Memorial Union Quad",
    "coordinate": [38.54141, -121.74845],
    "menu": {
        "Penne Pasta w/ Bolognese" : ["🌾","🐷"],
        "Penne Pasta w/ Roasted Mushrooms" : ["🌾","V"]
    }
  },
  {
    "day": "Tuesday",
    "locationName": "Worldwide Heart",
    "coordinate": [38.54540, -121.75494],
    "menu": {
        "Beef & Lamb Gyro" : ["🌾","🫛", "🥛"],
        "Edamame Hummus w/ Veggies" : ["🫛","🥛", "SE", "VE"]
    }
  },
  {
    "day": "Wednesday",
    "locationName": "Storer Corridor",
    "coordinate": [38.54114, -121.75461],
    "menu": {
        "Seafood Salad Tostada" : ["🥚","🦐", "🫛", "🐟"],
        "Southwest Black Bean Tostada" : ["V"]
    }
  },
  {
    "day": "Thursday",
    "locationName": "Orchard Park",
    "coordinate": [38.544828, -121.765170],
    "menu": {
        "Teriyaki Beef w/ Stir Fry Noodles" : ["🌾","🫛", "SE"],
        "Teriyaki Tofu w/ Veggie Stir Fry" : ["🌾","🫛", "SE","V"]
    }
  },
   {
    "day": "Friday",
    "locationName": "Memorial Union Quad",
    "coordinate": [38.54141, -121.74845],
    "menu": {
        "Soy Ciltrano Lime Rooster" : ["🌾","🫛"],
        "Southwest Tofu" : ["🫛","V"]
    }
  }
]

That is how I am modeling the information in Swift:

import Basis
import OrderedCollections

struct Menu : Codable, Hashable {
    var id: UUID { UUID() }
    let day: String
    let locationName: String
    let coordinate: [Double]
    let menu: OrderedDictionary<String, [String]>
    
    func getTodaysLocation(_ at the moment: String)-> String{
        if at the moment == day{
            return locationName
        }
        return ""
    }
    
    
}

And that is how I am decoding the information:

import Basis

extension Bundle {
    func decode(_ file: String) -> [Menu] {
        guard let url = self.url(forResource: file, withExtension: nil) else {
            fatalError("Did not find (file) in bundle.")
        }

        guard let knowledge = strive? Information(contentsOf: url) else {
            fatalError("Did not load (file) from bundle.")
        }

        let decoder = JSONDecoder()

        do {
            return strive decoder.decode([Menu].self, from: knowledge)
        } catch DecodingError.keyNotFound(let key, let context) {
            fatalError("Did not decode (file) from bundle resulting from lacking key '(key.stringValue)' – (context.debugDescription)")
        } catch DecodingError.typeMismatch(_, let context) {
            fatalError("Did not decode (file) from bundle resulting from sort mismatch – (context.debugDescription)")
        } catch DecodingError.valueNotFound(let sort, let context) {
            fatalError("Did not decode (file) from bundle resulting from lacking (sort) worth – (context.debugDescription)")
        } catch DecodingError.dataCorrupted(_) {
            fatalError("Did not decode (file) from bundle as a result of it seems to be invalid JSON.")
        } catch {
            fatalError("Did not decode (file) from bundle: (error.localizedDescription)")
        }
    }
}

However I get the next error: “Did not decode Menu.json from bundle resulting from sort mismatch – Anticipated to decode Array<Any> however discovered a dictionary as an alternative.”

Am I modeling the information accurately? Or is it a difficulty with the decode perform within the Bundle?

I attempted modifying the way in which I am modeling the information, however I new to JSON, so I am not completely certain if the problem is the way in which I modeled the JSON in Swift.

Related Articles

Social Media Auto Publish Powered By : XYZScripts.com