将json数据分配给标签

问题描述 投票:0回答:2

我是快速编程的初学者,我想将数据分配给标签

所以我在下面有此代码

@IBOutlet weak var Infected: WKInterfaceLabel!
    @IBOutlet weak var Cured: WKInterfaceLabel!
    @IBOutlet weak var Deaths: WKInterfaceLabel!

    @IBOutlet weak var OmanInfected: WKInterfaceLabel!
    @IBOutlet weak var OmanCured: WKInterfaceLabel!
    @IBOutlet weak var OmanDeaths: WKInterfaceLabel!


    func check()
    {


        struct CoronaData:Codable{
            var countrydata:String
           var total_active_cases:Int
           var total_cases:Int
           var total_danger_rank:Int
           var total_deaths:Int
           var total_new_cases_today:Int
           var total_new_deaths_today:Int
           var total_recovered:Int
           var total_serious_cases:Int
           var total_unresolved:Int
        }

        if let url = URL(string: "https://api.thevirustracker.com/free-api?countryTotal=OM") {
           URLSession.shared.dataTask(with: url) { data, response, error in
              if let data = data {
                  do {
                     let res = try JSONDecoder().decode(CoronaData.self, from: data)
                    print(res.countrydata)
                  } catch let error {
                     print(error)
                  }
               }
           }.resume()
        }

        AF.request("https://api.thevirustracker.com/free-api?countryTotal=OM").response {respond in debugPrint(respond)}

    }

并且我在控制台菜单中获得此数据

2020-04-17 01:53:07.248021+0400 Corona Virus WatchKit Extension[91305:3115960] [default] +[SPRemoteInterface controller:setupProperties:viewControllerID:tableIndex:rowIndex:classForType:]:2613: Unknown property in Interface description ('Died') for controller <Corona_Virus_WatchKit_Extension.InterfaceController: 0x8011b8e0>
[Request]: GET https://api.thevirustracker.com/free-api?countryTotal=OM
[Request Body]: 
None
[Response]: 
[Status Code]: 200
[Headers]:
Access-Control-Allow-Origin: *
Content-Encoding: br
Content-Type: application/json
Date: Thu, 16 Apr 2020 21:53:07 GMT
Server: cloudflare
Vary: Accept-Encoding
cf-cache-status: DYNAMIC
cf-ray: 585123262de729b0-MCT
cf-request-id: 0226944bdd000029b02f1ca200000001
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
[Response Body]: 
{
   "countrydata":[
      {
         "info":{
            "ourid":118,
            "title":"Oman",
            "code":"OM",
            "source":"https://thevirustracker.com/oman-coronavirus-information-om"
         },
         "total_cases":1019,
         "total_recovered":176,
         "total_unresolved":0,
         "total_deaths":4,
         "total_new_cases_today":109,
         "total_new_deaths_today":0,
         "total_active_cases":839,
         "total_serious_cases":3,
         "total_danger_rank":74}],
                "stat":"ok"}

[Data]: 530 bytes
[Network Duration]: None
[Serialization Duration]: 0.0s
[Result]: success(Optional(530 bytes))
typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "countrydata", intValue: nil)], debugDescription: "Expected to decode String but found an array instead.", underlyingError: nil))

现在如何将这些值分配给此处的标签(数字标签)

enter image description here

我知道这是一个愚蠢的问题,但请原谅我我只想完成程序并学习一些新事物

json swift alamofire apple-watch watchos
2个回答
0
投票

countrydata表示为数组,但是模型中的相应属性为String类型。


0
投票

您可以下载此免费程序来构建结构

enter image description here

请参见此example以使用struct

         if let url = URL(string: "https://api.thevirustracker.com/free-api?countryTotal=OM") {
           URLSession.shared.dataTask(with: url) { data, response, error in
              if let data = data {
                  do {
                     let decoder = JSONDecoder()
                     let profile = try decoder.decode(ProfileResponse.self, from: data)
                     print(profile)
                  } catch let error {
                     print(error)
                  }
               }
           }.resume()
        }

您可以使用setText(:)或setAttributedText( :)方法以编程方式更改文本。

© www.soinside.com 2019 - 2024. All rights reserved.