簡體   English   中英

將 JSON 解析為 Swift model

[英]Parse JSON to Swift model

{
    "location": {
        "height": "0",
        "time": [
            {
                "solarnoon": {
                    "desc": "LOCAL DIURNAL MAXIMUM SOLAR ELEVATION (Max= 25.86714)",
                    "time": "2020-12-22T11:54:55-05:00",
                    "elevation": "25.867141373"
                },
        
                "sunset": {
                    "time": "2020-12-22T16:32:35-05:00",
                    "desc": "LOCAL DIURNAL SUN SET"
                },
                "sunrise": {
                    "desc": "LOCAL DIURNAL SUN RISE",
                    "time": "2020-12-22T07:17:15-05:00"
                },
        ],
        "longitude": "-74",
        "latitude": "40.7"
    },
    "meta": {
        "licenseurl": "https://api.met.no/license_data.html"
    }
}

我有這個 JSON 代碼,我想把它解析成 Swift model 我試過了,但它似乎是錯誤的。 任何幫助表示贊賞。 我必須添加更多細節,所以我已經嘗試解決這個問題 2 個小時了,但我無法理解我做錯了什么。

我的嘗試:

struct SunResponse: Decodable {
    var location: SunriseDetail
}

struct SunriseDetail: Decodable {
    var name = "Sunrise"
    var sunrise: TimeInfo
}

struct TimeInfo: Decodable {
    var time: String
}
{
    "location": {
        "height": "0",
        "time": [
            {
                "solarnoon": {
                    "desc": "LOCAL DIURNAL MAXIMUM SOLAR ELEVATION (Max= 25.86714)",
                    "time": "2020-12-22T11:54:55-05:00",
                    "elevation": "25.867141373"
                },
        
                "sunset": {
                    "time": "2020-12-22T16:32:35-05:00",
                    "desc": "LOCAL DIURNAL SUN SET"
                },
                "sunrise": {
                    "desc": "LOCAL DIURNAL SUN RISE",
                    "time": "2020-12-22T07:17:15-05:00"
                },
        ],
        "longitude": "-74",
        "latitude": "40.7"
    },
    "meta": {
        "licenseurl": "https://api.met.no/license_data.html"
    }
}

我有這個 JSON 代碼,我想把它解析成 Swift model 我試過了,但它似乎是錯誤的。 任何幫助表示贊賞。 我必須添加更多細節,所以我已經嘗試解決這個問題 2 個小時了,但我無法理解我做錯了什么。

我的嘗試:

struct SunResponse: Decodable {
    var location: SunriseDetail
}

struct SunriseDetail: Decodable {
    var name = "Sunrise"
    var sunrise: TimeInfo
}

struct TimeInfo: Decodable {
    var time: String
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM