gpt4 book ai didi

ios - 不从 JSON 调用中获取数据

转载 作者:行者123 更新时间:2023-11-28 07:04:41 25 4
gpt4 key购买 nike

我出于学习目的创建了一个小型应用程序,它应该从 JSON 文件中获取数据并将其显示在表格 View 中。

我不确定如何从 JSON 访问我的数据。如果有人能帮助我,我会更高兴。

[
{
"title": "dolore laboris ea adipisicing",
"description": "Ullamco adipisicing aute deserunt id do eu aliqua deserunt deserunt quis elit. Irure excepteur aliquip mollit tempor. Minim est nisi aute do tempor nisi aliquip pariatur. Laboris qui duis consequat magna qui. Proident nisi duis Lorem officia eiusmod exercitation aliqua dolore sunt esse sunt consectetur ut.",
"images": [
{
"index": 0,
"title": "laborum minim deserunt elit",
"pubDate": 1421120486130,
"picture": "http://lorempixel.com/500/500/nightlife/",
"caption": "deserunt proident cillum laboris duis ut minim est"
},

这是我想要访问属性标题和图片的 JSON 文件。

我在做什么:

我有 Parser 类来访问 JSON

与基本 url 保持一致

一个正在调用的函数

    func getRandomUser(onCompletion: (JSON) -> Void) {
let route = baseURL
makeHTTPGetRequest(route, onCompletion: { json, err in
onCompletion(json as JSON)
})
}

and a normal **makeHTTPGetRequest** function.


And this is how i try to fill my table

func addDummyData() {
RestParser.sharedInstance.getTableViewData { json in
let results = json["images"]
for (index: String, subJson: JSON) in results {
let user: AnyObject = subJson["images"].object
self.items.addObject(user)
dispatch_async(dispatch_get_main_queue(),{
tableView?.reloadData()
})
}
}
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell

if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}

let user:JSON = JSON(self.items[indexPath.row])

let picURL = user["picture"].string
let url = NSURL(string: picURL!)
let data = NSData(contentsOfURL: url!)

cell!.textLabel?.text = user["title"].string
cell?.imageView?.image = UIImage(data: data!)


return cell!
}

没有太大的成功..我没有得到任何返回

我如何尝试调用数据

func getTableViewData(onCompletion:(JSON) -> Void){
let route = baseURL
makeHTTPGetRequest(route, onCompletion: { json, err in
onCompletion(json as JSON)
})
}

最佳答案

看来您正在使用 SwiftyJSON 类来处理 JSON 字符串。

这里的解决方案假设如下:

  • 具有此声明的变量

    var items : [JSON]() 
  • RestParser.sharedInstance.getTableViewData 函数返回 SwiftyJSON 兼容数据

  • 返回的 SwiftyJSON 对象是一个字典,它包含一个包含一些属性的数组images

addDummyData 函数遍历图像数组并将 JSON.dictionaries 对象添加到 items

  func addDummyData() {
RestParser.sharedInstance.getTableViewData { json in
let results = json["images"]
for (index: String, user: JSON) in results {
items.append(user)
}
dispatch_async(dispatch_get_main_queue(),{
tableView?.reloadData()
})
}
}

tableView:cellForRowAtIndexPath 函数照常创建单元格,并将属性 titlepicture 分配给适当的单元格属性。

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell

if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}

let user = items[indexPath.row]

if let picURL = user["picture"].string, url = NSURL(string: picURL) {
if let data = NSData(contentsOfURL: url) {
cell!.imageView?.image = UIImage(data: data)
}
}
cell!.textLabel?.text = user["title"].string

return cell!
}

关于ios - 不从 JSON 调用中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31020054/

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