gpt4 book ai didi

json - Swift:如何从 JSON 数据读取 JSON 数据

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

我已经从 JSON 读取了一些数据,其中包括另一个 JSON 文件的 url,我需要读取该 JSON 文件并存储它们。我不知道应该在哪里添加读取JSON的第二个过程。我对 swift 很陌生,有人可以给我一些建议吗?非常感谢。

var posts: [Posts] = [Posts]()

@IBOutlet weak var theCollectionView: UICollectionView!
func getCategories(){

Alamofire.request(.GET, "http://localhost:8888/wordpress/wp-json/wp/v2/categories").responseJSON { (response) in
if let jsonFile = response.result.value {

var arrayOfPosts = [Posts]()

for post in jsonFile as! NSArray {

let postObj = Posts()

// if !(post.valueForKeyPath("featured_image_thumbnail_url") is NSNull){

// postObj.postThumbnailUrlString = post.valueForKeyPath("featured_image_thumbnail_url") as! String
postObj.postCategoryCode = "\(post.valueForKeyPath("id") as! NSNumber)"
postObj.theDictionary = post.valueForKeyPath("_links") as! NSDictionary
postObj.theCategoryArray = (postObj.theDictionary.valueForKeyPath("wp:post_type") as? NSArray)!
postObj.postCategoryListUrl = postObj.theCategoryArray[0].valueForKeyPath("href") as! String

arrayOfPosts.append(postObj)



}

self.posts = arrayOfPosts
self.theCollectionView.reloadData()


}
}
}

class Posts: NSObject{

var postTite:String = ""
var postContent: String = ""
var postCategoryCode: String = ""
var postThumbnailUrlString: String = ""
var theCategoryArray = NSArray()
var theDictionary = NSDictionary()
var postCategoryListUrl: String = ""
}

我正在使用Alamofire,在得到“postObj.postCategoryListUrl”后我尝试添加另一个循环,但它不起作用。我需要使用“postObj.postCategoryListUrl”的url来读取“postTitle”、“postThumbnailUrlString”和“postContent”的数据。我对 swift 很陌生,需要一些建议,非常感谢。

最佳答案

您可以对本行之后的第二个 json 执行 Alamife.request

self.posts = arrayOfPosts

当第二个 json 数据钱包完成时,请使用此行。然后您将获得完整的帖子和帖子详细信息。

dispatch_async(dispatch_get_main_queue(),{
self.theCollectionView.reloadData()
})

关于json - Swift:如何从 JSON 数据读取 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38942043/

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