Json decoder

Hi, my Json file looks like that:

{"Synapses": [["ID: 0", "XPosition: 387.8125", "YPosition: 502.96875", "Width: 187.375", "Height: 122.0625"],["ID: 16", "XPosition: 490.859375", "YPosition: 947.15625", "Width: 49.28125", "Height: 69.6875"]]}

Ps: i deleted the arrays from 2 to 15. its nearly all the same.

and thats my load function:

func load(){//fileName: String) {
     
    if let filemanger = Bundle.main.url(forResource: "Synapses", withExtension: "json"){
       
      do{
        let data = try Foundation.Data(contentsOf: filemanger)
        let jsondecoder = JSONDecoder()
        let datafromjson = try jsondecoder.decode([SynapsData].self, from: data)
         
        self.Data = datafromjson
      }catch{
        print("fuck ey so ne scheisse\(error)")
      }
    }
  }
struct SynapsData: Codable {
  var ID : Int
  var SynapsX : CGFloat
  var SynapsY : CGFloat
  var SynapsWidth : CGFloat
  var SynapsHeight : CGFloat
}

And my Output is just []. I just don't see the problem 😅.

Try with these as your Codable structs:

struct Synapses: Codable {

    var synapses: [SynapsData]

}

struct SynapsData: Codable {

  var id : Int

  var xPosition : CGFloat

  var yPosition : CGFloat

  var width : CGFloat

  var height : CGFloat

}

and this as your JSON file:

{

    "synapses": [

        {

            "id": 0,

            "xPosition": 387.8125,

            "yPosition": 502.96875,

            "width": 187.375,

            "height": 122.0625

        },

        {

            "id": 16,

            "xPosition": 490.859375,

            "yPosition": 947.15625,

            "width": 49.28125,

            "height": 69.6875

        }

    ]



}

then decode Synapses.self from the data of your JSON file

your json is not equal to your construct.

this Json

{"Synapses": [["ID: 0", "XPosition: 387.8125", "YPosition: 502.96875", "Width: 187.375", "Height: 122.0625"],["ID: 16", "XPosition: 490.859375", "YPosition: 947.15625", "Width: 49.28125", "Height: 69.6875"]]}

is equal to the following construct

struct NAME: Codable {
    let synapses: [[String]]

    enum CodingKeys: String, CodingKey {
        case synapses = "Synapses"
    }
}

in order to match your json to this construct

// MARK: - Synapse
struct Synapse: Codable {
    let id: Int
    let synapsX, synapsY, synapsWidth, synapsHeight: Double

    enum CodingKeys: String, CodingKey {
        case id = "ID"
        case synapsX = "SynapsX"
        case synapsY = "SynapsY"
        case synapsWidth = "SynapsWidth"
        case synapsHeight = "SynapsHeight"
    }
}

the json should be like

{
  "Synapses": [
    {
      "ID": 0,
      "SynapsX": 387.8125,
      "SynapsY": 502.96875,
      "SynapsWidth": 187.375,
      "SynapsHeight": 122.0625
    },
    {
      "ID": 16,
      "SynapsX": 490.859375,
      "SynapsY": 947.15625,
      "SynapsWidth": 49.28125,
      "SynapsHeight": 69.6875
    }
  ]
}

Summery: your json did not match your construct from many point of views

Json decoder
 
 
Q