V. Khuraskin
V. Khuraskin

Reputation: 89

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!

Upvotes: 0

Views: 83

Answers (1)

Nima Yousefi
Nima Yousefi

Reputation: 817

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

var files: [String: DetailGist]?

Upvotes: 1

Related Questions