2016-03-14 60 views
0

我有一個JSON應用程序,但我認爲我有我的JSON代碼的問題。我在不同的驗證器上檢查了我的JSON,他們都說我的JSON是有效的。這是我的錯誤:與iOS操作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 
} 
} 
+0

此頁面返回HTML,而不是JSON。它在瀏覽器中顯示JSON,因爲該頁面具有用JSON替換HTML的JavaScript。這是問題:您的代碼獲取HTML,而不是JSON。 – Moritz

+0

@EricD。那麼我應該如何將它解釋爲JSON? –

+0

您必須從提供JSON的端點獲取數據,而不是從使用JavaScript構建的動態HTML頁面獲取數據。問題不在於你的代碼,而在於源代碼。 – Moritz

回答

0

看着你的代碼和調試後,它看起來是你有額外的空間在你的迴應中啓動每一個關鍵。這表明服務器端的json編碼器有問題。可能這是alamofire中的bug,在服務器端與django一起使用它。

+0

這是有道理的。我剛換了我的FTP服務器,而我以前使用的服務器似乎工作,但不是這個。 –

+0

我們在瀏覽器*中獲得的JSON響應正常*,您可以使用http://jsonlint.com/進行驗證。 – Moritz

+0

我已驗證它,並罰款。 @EricD。 –