-2

I am attempting to parse the following json data:

enter image description here

Below is my struct:

struct Album: Decodable {
    var source: [Sourcet]
    enum CodingKeys: String, CodingKey {
        case source = "_source"
    }
}

struct Sourcet: Decodable {
    var nome, endereco, uf, cidade, bairro: String
}

let response = try JSONDecoder().decode(Album.self, from: data)

I continue getting the error:

keyNotFound(CodingKeys(stringValue: "_source", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"_source\", intValue: nil) (\"_source\").", underlyingError: nil))

Is this due to the json information being an array?.How would I be able to parse this information?

Krunal Nagvadia
  • 1,083
  • 2
  • 12
  • 33
  • Possible duplicate of [Swift : The data couldn’t be read because it isn’t in the correct format](https://stackoverflow.com/questions/55030573/swift-the-data-couldn-t-be-read-because-it-isn-t-in-the-correct-format) – staticVoidMan Mar 08 '19 at 11:19

1 Answers1

3

Your struct Album is wrong and you're parsing Album.self single object instead of array.

Try below code :

struct Album: Decodable {
    var source: Sourcet // change array to single object
    enum CodingKeys: String, CodingKey {
        case source = "_source"
    }
}

struct Sourcet: Decodable {
    var nome, uf : String
}

To parse json in model :

do {
      let response = try JSONDecoder().decode([Album].self, from: data)
      for item in response {
          print(item.source.nome)
      }
   }catch{
          print("Error: ",error)
   }
Pratik Prajapati
  • 1,137
  • 1
  • 13
  • 26
  • can you please write the complete code for call the above json. It is REST Api with Post method – Krunal Nagvadia Mar 08 '19 at 04:44
  • add json as text instead of image – Pratik Prajapati Mar 08 '19 at 04:46
  • https://drive.google.com/file/d/1V25FAlCQRYNKVAp695SFOzQlLFO8AsIA/view?usp=sharing – Krunal Nagvadia Mar 08 '19 at 04:52
  • i checked this code with your data, its working fine. check what you received as data. add error log and api response for more help – Pratik Prajapati Mar 08 '19 at 06:13
  • Oh thanks for the effort. `Error dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Unable to convert data to string around character 2272." UserInfo={NSDebugDescription=Unable to convert data to string around character 2272.})))` this is the error – Krunal Nagvadia Mar 08 '19 at 06:29
  • error itself describing `The given data was not valid JSON.` whatever data you receive is not complete so how can code work! , to solve check your api in postman or use json parser to verify response is in correct formate. response must be in json format to work above code. – Pratik Prajapati Mar 08 '19 at 06:56
  • With use of do and catch there is also a good advantage that is we can check any error if there is issue with key missing or key type missing in your Model. you can print error in the catch block and catch your model issue very quickly. As in my case, I was using integer type instead of string type value. And with help of catch, I found this issue within 5 seconds. – Ravi Nov 25 '20 at 12:45