I have an app, where I should parse all the data from local JSON file to my label, songs. There is a JSON file, with one array, with two arrays. For example, I need to use "loop data" in one of the view controller, and "beatloopsdata" in another. When I run this, I’ve got an error, that data corrupted. There is my JSON File:
{
"beatpackdata": [
{
"loopsdictionary": [
{
"nameOfLoop": "Away we go",
"nameOfProducer": "Tubular Kingz",
"countOfLoops": "28",
"genreOfLoops": "Lo-fi Hip Hop"
},
{
"nameOfLoop": "Test",
"nameOfProducer": "Testing",
"countOfLoops": "25",
"genreOfLoops": "Lo-fi"
}
]
},
{
"beatloopsdictionary": [
{
"loopName" : "Alien",
"instrument" :"Arp",
"songName" : "alienarpjason.mp3",
"producer" : "Stefan Guy"
},
{
"loopName" : "Big Brake",
"instrument" :"Drums",
"songName" : "BigBrake_Drums_Jason.mp3",
"producer" : "Stefan Guy"
},
{
"loopName" : "Bongo Beats",
"instrument" :"Drums",
"songName" : "BongoBeats_Drums_Jason.mp3",
"producer" : "Stefan Guy"
},
{
"loopName" : "Dreaming",
"instrument" :"Keys",
"songName" : "Dreaming_Keys_Jason.mp3",
"producer" : "Stefan Guy"
},
{
"loopName" : "Funky Groove",
"instrument" :"Bass",
"songName" : "FunkyGroove_Bass_Jason.mp3",
"producer" : "Stefan Guy"
},
{
"loopName" : "Futurist",
"instrument" :"Arp",
"songName" : "Futurist_Arp_Jason.mp3",
"producer" : "Stefan Guy"
},
{
"loopName" : "Hoping for change",
"instrument" :"Arp",
"songName" : "HopingForChange_Arp_Jason.mp3",
"producer" : "Stefan Guy"
},
{
"loopName" : "Manic",
"instrument" :"Bass",
"songName" : "Manic_Bass_Jason.mp3",
"producer" : "Stefan Guy"
},
{
"loopName" : "Sassy",
"instrument" :"Drums",
"songName" : "Sassy_Drums_Jason.mp3",
"producer" : "Stefan Guy"
},
{
"loopName" : "Serious",
"instrument" :"Arp",
"songName" : "Serious_Arp_Jason.mp3",
"producer" : "Stefan Guy"
},
{
"loopName" : "Stable Bricks",
"instrument" :"Bass",
"songName" : "StableBrick_Bass_Jason.mp3",
"producer" : "Stefan Guy"
},
{
"loopName" : "Thump",
"instrument" :"Drums",
"songName" : "Thump_Drums_Jason.mp3",
"producer" : "Stefan Guy"
},
{
"loopName" : "Tropic",
"instrument" :"Drums",
"songName" : "TropicVibe_Drums_Jason.mp3",
"producer" : "Stefan Guy"
}
]
}
]
}
I’ve created a model, but not sure that all is correct. I’ve tried my best, but it’s third time when I use a local JSON file:
import Foundation
struct BeatData: Codable {
let beatpackdata: [BeatPackData]
}
struct BeatPackData: Codable {
let loopdata: [BeatLoopsData]
let beatloopsdata: [LoopData]
}
struct LoopData: Codable {
let loop_name: String
let Instrument: String
let song_name: String
let producer: String
}
struct BeatLoopsData: Codable {
let nameOfLoop: String
let nameOfProducer: String
let countOfLoops: String
let genreOfLoops: String
}
Parse Function:
private func parseJSON() {
guard let path = Bundle.main.path(forResource: "beatpackdata", ofType: "json") else {
return
}
let url = URL(fileURLWithPath: path)
do {
let jsonData = try Data(contentsOf: url)
beatPackData = try JSONDecoder().decode(BeatData.self, from: jsonData)
if let beatPackData = beatPackData {
print(beatPackData)
}
}
catch {
print(error)
}
return
}
}
2
Answers
I put the json data into quicktype, and used the generated data structures in the code.
So here is the working code. You can tell your teacher, you did the school assignment all on your own.
EDIT 1: here is the data I used, I put this data into a file called "beatpackdata.json" in my xcode project.
Now Your Josn Is look like
Model File
How To Use