如何使用Swift在折线图中显示JSON数据

时间:2019-03-26 12:15:47

标签: ios json

我是ios应用程序的新手,我目前正在设计一个读取json数据的应用程序。 当前的设计挑战是要捕获json数据。我不知道如何使用“图表”库创建折线图来显示json数据。

此刻,我只是将json转移到数组中。

@objc func getlatestTh_5min(){
      guard let th_5minUrl = URL(string: kivaLoanURl) else{
          return
      }

      let request = URLRequest(url: th_5minUrl)
      let task = URLSession.shared.dataTask(with: request, completionHandler: {(data,response,error) -> Void in

          if let error = error {
              print(error)
              return
          }
          if let data = data {
              self.th_5mins = self.pardrJsonData(data: data)
          }
      })
      task.resume()
  }
  func pardrJsonData(data: Data) -> [Th_5min]{
      var th_5mins = [Th_5min]()
      do {
          let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary

          let jsonTh_5mins = jsonResult?["TH_5min"] as! [AnyObject]
          for jsonTh_5min in jsonTh_5mins{
              var th_5min = Th_5min()
              th_5min.XTP_A = jsonTh_5min["XTP_A"] as! Double
             // th_5min.XHP_A = jsonTh_5min["XHP_A"] as! Double
              th_5min.XTP_B = jsonTh_5min["XTP_B"] as! Double
             // th_5min.XHP_B = jsonTh_5min["XHP_B"] as! Double
              th_5min.XTP_C = jsonTh_5min["XTP_C"] as! Double
              //th_5min.XHP_C = jsonTh_5min["XHP_C"] as! Double
              th_5min.XTP_D = jsonTh_5min["XTP_D"] as! Double
              //th_5min.XHP_D = jsonTh_5min["XHP_D"] as! Double
              th_5min.XTP_E = jsonTh_5min["XTP_E"] as! Double
              //th_5min.XHP_E = jsonTh_5min["XHP_E"] as! Double
              //alarm_event.Al_Message = jsonAlarm_Event["Al_Message"] as! String

              th_5mins.append(th_5min)
          }        }catch{
              print(error)
      }
      return th_5mins
  }

我想使用折线图显示json数据,但我不知道如何开始。

enter image description here

这是我来自网站的json数据

{
TH_5min: [
{
Data: "2019-02-23T00:00:00",
Time: "11:00:00",
XTP_A: 10.5,
XHP_A: 11.5,
XTP_B: 33.5,
XHP_B: 44.6,
XTP_C: 88.9,
XHP_C: 66.6,
XTP_D: 77.9,
XHP_D: 99.6,
XTP_E: 87.87,
XHP_E: 66.66
},
{
Data: "2019-02-23T00:00:00",
Time: "11:05:00",
XTP_A: 55.2,
XHP_A: 44.3,
XTP_B: 66.6,
XHP_B: 77.87,
XTP_C: 87.77,
XHP_C: 87.87,
XTP_D: 8.87,
XHP_D: 78.78,
XTP_E: 87.78,
XHP_E: 87.87
}
]
}

0 个答案:

没有答案