gpt4 book ai didi

json - 尝试解析 JSON 数据以放入另一个 JSON url 来解析更多数据

转载 作者:行者123 更新时间:2023-11-30 10:54:02 25 4
gpt4 key购买 nike

所以情况是这样的,我试图解析链接 1 中的 JSON 数据,并将其放入 URL 中以解析链接 2 中的 JSON 数据。我为此创建了两个结构。第一个结构是我的收集(其中我必须使用我的 id 来解析第二个链接中的其他 JSON)

struct Collects: Codable {
let collects: [Collect]
}

struct Collect: Codable {
let productID: Int

enum CodingKeys: String, CodingKey {
case productID = "product_id"
}
}

我的第二个结构包含产品(将从第二个链接解析)

struct Products: Codable {
let products: [Product]
}

struct Product: Codable {
let title: String
enum CodingKeys: String, CodingKey {
case title
}
}

Product 结构中的标题是产品的名称。在我的 View Controller 中,我有以下两个用于获取 JSON 的函数:

func fetchJSON(url: String, completion: @escaping (Collects?, Error?) -> Void) {
guard let url = URL(string: url) else { return }

let session = URLSession.shared
let dataTask = session.dataTask(with: url) { (data, response, error) in
if let error = error {
completion(nil, error)
print("Unable to fetch data", error)
}
guard let data = data else { return }
do {
let response = try JSONDecoder().decode(Collects.self, from: data)
DispatchQueue.main.async {
completion(response, nil)
}
} catch let jsonError{
print("Unable to decode: ", jsonError)
}
}
dataTask.resume()
}

func fetchProducts(url: String, completion: @escaping (Products?, Error?) -> Void) {
guard let url = URL(string: url) else { return }

let session = URLSession.shared
let dataTask = session.dataTask(with: url) { (data, response, error) in
if let error = error {
completion(nil, error)
print("Unable to fetch data", error)
}
guard let data = data else { return }
do {
let response = try JSONDecoder().decode(Products.self, from: data)
DispatchQueue.main.async {
completion(response, nil)
}
} catch let jsonError{
print("Unable to decode: ", jsonError)
}
}
dataTask.resume()
}

第一个用于解析 id,第二个用于解析产品。在我的 viewDidLoad 中,我使用此信息来获取数据并设置它,以便可以将产品放入 Collection View 中。

在我看来DidLoad我有以下

let jsonURL = "https://xxxxxxxxxxxxx"


//We need to first fetch the collects
fetchJSON(url: jsonURL) { (response, error) in
guard let item = response?.collects else { return }
self.collects = item
for i in self.collects {
//This will put all the id's together
self.collectionCollects.append(",")
self.collectionCollects.append(String(i.productID))
}
//This will remove the first comma in the string
self.collectionCollects.remove(at: self.collectionCollects.startIndex)

//This is the productURL that we will use to retrieve the products and put them in the collectionView
let productURL = "https://xxxxxxxxxxxxx\(self.collectionCollects)&page"

self.fetchProducts(url: productURL, completion: { (response, error) in
guard let item = response?.products else {return}
self.products = item
self.collectionView.reloadData()
//The products get printed here
print(self.products)
}
)
}

为了稍微解释一下上面的内容,JSONUrl 是 JSON 数据的第一个 url,它被传递到函数中以获取信息。在第一个 for 循环中,我所做的是将 id 放入字符串中并用逗号分隔,然后删除第一个逗号。之后,我调用第二个 JSON 函数以使用新 URL 来获取第二个 URL 的产品。在语句“print(self.products)”中,我得到了我期望的产品列表,即使我在 self.fetchProducts 中执行 for 循环来迭代并打印出产品,我也得到了预期的结果。然而,当我尝试使用数据设置我的 Collection View 时,它只是一个没有信息的白屏。我认为问题在于我如何设置它,而不是我的单元的配置。

我忘记提及的一件事是,在我的 View Controller 中我有以下数组:

var collects = [Collect]()
var collectionCollects = ""
//This will have the products
var products = [Product]()

我真的被困住了,我不确定最好是在一个 View Controller 中完成所有这些操作还是将其拆分,但它必须是一个 View 。

最佳答案

您的问题是关于 Collection View 的。我建议看看here 。如果当您迭代时得到正确的响应,那么它设置的 Collection View 就是您的问题。

关于json - 尝试解析 JSON 数据以放入另一个 JSON url 来解析更多数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54156342/

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