gpt4 book ai didi

iOS 操作时出现 JSON 错误

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

我有一个 JSON 应用程序,但我认为我的 JSON 代码有问题。我已经在不同的验证器上检查了我的 JSON,他们都说我的 JSON 是有效的。这是我的错误:

Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}

我的 JSON 已上传到 http://www.annabellesykes.byethost11.com/shailenewoodleyfansappjson.json

我的JSON处理代码是:

class NewsTableViewController: UITableViewController, UIDocumentInteractionControllerDelegate {

var siteURL = "http://www.annabellesykes.byethost11.com/shailenewoodleyfansappjson.json"
var items = [Item]()
var item:Item!

override func viewDidLoad() {
super.viewDidLoad()

getLatestNews()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NewsTableViewCell

// Configure the cell...
cell.titleLabel.text = items[indexPath.row].title
cell.dateLabel.text = items[indexPath.row].date
cell.contentLabel.text = items[indexPath.row].content

cell.titleLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.titleLabel.numberOfLines = 999
cell.contentLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.contentLabel.numberOfLines = 3
cell.dateLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.dateLabel.numberOfLines = 999

cell.dateLabel.sizeToFit()

return cell
}

func getLatestNews() {

let request = NSURLRequest(URL: NSURL(string: siteURL)!)
let urlSession = NSURLSession.sharedSession()
let task = urlSession.dataTaskWithRequest(request, completionHandler: {
(data, response, error) -> Void in

if let error = error {
print(error)
return
}

if let data = data {
self.items = self.parseJsonData(data)

NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
self.tableView.reloadData()
})
}

})

task.resume()
}

func parseJsonData(data: NSData) -> [Item] {

var items = [Item]()

do {

let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as? NSDictionary

let jsonItems = jsonResult?["items"] as! [AnyObject]
for jsonItem in jsonItems {
let item = Item()
item.title = jsonItem["title"] as! String
item.content = jsonItem["content"] as! String
item.date = jsonItem["date"] as! String
items.append(item)
}

} catch {
print(error)
}

return items
}
}

最佳答案

查看您的代码并在调试后发现您的响应中每个键的开头都有额外的空间。这说明服务器端的json编码器有问题。这可能是 alamofire 与服务器端 django 一起使用时出现的错误。

关于iOS 操作时出现 JSON 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35986786/

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