SWIFT model for parsing JSON


SWIFT model for parsing JSON



I need to parse the data of JSON. For this I will use the protocol Codable.
The json received looks like this (this is the part that interests me):


(
{
description = mySecondGist;
files = {
"gistfile1.txt" = {
filename = "gistfile1.txt";
language = Text;
"raw_url" = "https://gist.githubusercontent.com/VladimirKhuraskin/9ca2362c09cebcc16bd74f51f267231a/raw/74caacd3ad3eedb369a07b926327d2ef37e3eefc/gistfile1.txt";
size = 17;
type = "text/plain";
};
};
}
)



I made this model:


struct Gists: Codable {
var description: String?
var files: DetailGist?

private enum CodingKeys: String, CodingKey {
case description
case files
}
}

struct DetailGist: Codable {
var filename: String?
var rawUrl: String?

private enum FileCodingKeys: String, CodingKey {
case filename
case rawUrl = "raw_url"
}
}



Is this the right model? Or it needs to be finalized? I'm confused by the


files = {
"gistfile1.txt" =



thanks!





@Connor That is the output of printing the JSON converted to an array using JSONSerialization.jsonObject. Nima's answer below is correct.
– rmaddy
Jun 29 at 18:20


JSONSerialization.jsonObject





you can use json4swift.com to generate models
– Mihir Dave
Jun 29 at 18:28




1 Answer
1



No, files is a dictionary. That's what the {} markers in JSON mean. You want your Gists model to be


{}


var files: [String: DetailGist]?






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV