繁体   English   中英

尝试解析JSON数据以放入另一个JSON网址以解析更多数据

[英]Trying to parse JSON data to put in another JSON url to parse more data

所以这是一种情况,我试图解析来自链接1的JSON数据,并将其放在url中以解析来自链接2的JSON数据。为此,我创建了两个结构。 第一个结构是我的collect(在第二个链接中必须用来解析其他JSON的我的ID)

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
    }
}

产品结构中的标题是产品的名称。 在我的视图控制器中,我具有以下两个用于获取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中,我正在使用此信息来获取数据并进行设置,以便可以将产品放入收藏夹视图中。

在我的viewDidLoad中,我有以下内容

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循环以启动并打印出产品,也得到了预期的结果。 但是,当我尝试使用数据设置集合视图时,它只是一个没有信息的白屏。 我认为问题出在我的设置方式,而不是单元的配置。

我忘记提及的一件事是,在我的视图控制器中,我具有以下数组:

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

我真的很困惑,我不确定是否最好在一个视图控制器中完成所有这些工作或将其拆分,但是必须是一个视图。

您的问题是关于集合视图的。 我建议在这里看看。 如果您在进行迭代时得到正确的响应,则说明问题出在设置收集视图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM