gpt4 book ai didi

json - 可编码的 Swift JSON 解析嵌套的 JSON

转载 作者:行者123 更新时间:2023-12-04 01:53:11 26 4
gpt4 key购买 nike

我有看起来像这样的 JSON 数据

{
"latitude": 34.088051,
"longitude": -118.296512,
"timezone": "America/Los_Angeles",
"daily": {
"summary": "No precipitation throughout the week, with high temperatures rising to 92°F on Friday.",
"icon": "clear-day",
"data": [
{
"time": 1535266800,
"summary": "Mostly cloudy in the morning.",
"icon": "partly-cloudy-day",
"sunriseTime": 1535289854,
"sunsetTime": 1535336915,
"moonPhase": 0.51}

我想从中提取时间、摘要和图标。

我创建了一个 WeatherObject 类型的类,但这就是我卡住的地方。如何创建结构?键不是蛇形或命名错误,所以我想我可以不使用枚举重命名变量名。

编辑:做 - 捕获掉下来捕获。 VC实现协议(protocol)。传递给协议(protocol)方法的数据在方法实现中打印,但在 do 子句中无法处理。
protocol NetworkingDelegate: AnyObject {
func didGetResult(data: Data?) -> Void
}


class Networking: NSObject {

var data: Data?
weak var delegate: NetworkingDelegate?

func makeAPIRequest(apiString: String) -> Void {
guard let url: URL = URL(string: apiString) else{
print("ERROR no URL")
return
}

let urlRequest = URLRequest(url: url)

let dataSession = URLSession.shared
let dataTask = dataSession.dataTask(with: urlRequest) { (data, response, error) in

guard error == nil else{
print("ERROR found in networking call")
print(error!)
return
}

if data != nil{

print("Data exists in networking")
self.delegate?.didGetResult(data: data!)




}else{
print("ERROR: did not receive data")
}
}

dataTask.resume()
}


}

在 View Controller 中
  func didGetResult(data: Data?) {



do {

if let dataObject = data{

let theString:NSString = NSString(data: dataObject, encoding: String.Encoding.ascii.rawValue)!
print(theString)
}
let newJSONDecoder = JSONDecoder()
let weatherObject = try newJSONDecoder.decode(WeatherObj.self, from:data!)
let dailyObject = weatherObject.daily
let dataArray = dailyObject.data
//LOOP THROUGH dataArray and get the required info
for val in dataArray {
print("=======")
print(val.time)
print(val.summary)
print(val.icon)
print("=======")
}
} catch {
print("error while parsing:\(error.localizedDescription)")
}


}

最佳答案

您可以像这样创建结构

struct WeatherObject: Codable {
let latitude: Double
let longitude: Double
let timezone: String
let daily: Daily
}

struct Daily: Codable {
let summary: String
let icon: String
let data: [Datum]
}

struct Datum: Codable {
let time: Int
let summary: String
let icon: String
let sunriseTime: Int
let sunsetTime: Int
let moonPhase: Double
}

然后你可以解析它并获取所需的信息
do {
let newJSONDecoder = JSONDecoder()
let weatherObject = try newJSONDecoder.decode(WeatherObject.self, from: jsonData)
let dailyObject = weatherObject.daily
let dataArray = dailyObject.data
//LOOP THROUGH dataArray and get the required info
for val in dataArray {
print("=======")
print(val.time)
print(val.summary)
print(val.icon)
print("=======")
}
} catch {
print("error while parsing:\(error.localizedDescription)")
}

希望能帮助到你

关于json - 可编码的 Swift JSON 解析嵌套的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52032813/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com