最后的垃圾。“UserInfo = {NSDebugDescription =结尾的垃圾--->数据无法读取,因为它的格式不正确iOS Swift

时间:2016-10-03 12:05:27

标签: ios arrays json swift alamofire

所以这是我的网络请求。

// MARK: - 网络请求

let urlString = Constants.kBaseUrl + Constants.kEventsUrl

Alamofire.request(.GET, urlString, parameters: nil, encoding: .JSON, headers: [Constants.kChecksum:Constants.kChecksumValue]) .responseJSON { response in

    guard response.result.isSuccess else {

        let alertController = UIAlertController(title:"Error", message:response.result.error?.localizedDescription, preferredStyle:UIAlertControllerStyle.Alert)

        let okAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { action in
            self.isFetchingEvents = false;
            self.eventsFetchError = response.result.error
            self.eventsTableView.reloadData()
        })

        let retryAction = UIAlertAction(title: "Retry", style: .Default, handler: { action in
            self.downloadEvents()
        })

        alertController.addAction(okAction)
        alertController.addAction(retryAction)

        self.presentViewController(alertController, animated: true, completion:  nil);

        return
    }

    let tempInfo = response.result.value![Constants.kData] as! [AnyObject]

// MARK: - 来自邮递员的json回复

 {
  "success": 1,
  "message": "Successful",
  "data": [
    {
      "id": "196",
      "title": "Arusha Christmas Fair 2016",
      "body": null,
      "field_events_date_from": "2016-12-24",
      "field_events_date_to": "2016-12-25",
      "field_events_time_from": "2016-10-03 13:56",
      "field_events_time_to": "2016-10-03 21:57",
      "image": "http://clients.view9.com.np/jwaremap/sites/default/files/events/2250x3324-christmas-fair_0.png",
      "image-thumbnail": "http://clients.view9.com.np/jwaremap/sites/default/files/styles/thumbnail/public/events/2250x3324-christmas-fair_0.png?itok=U2W8DgA-",
      "image-medium": "http://clients.view9.com.np/jwaremap/sites/default/files/styles/medium/public/events/2250x3324-christmas-fair_0.png?itok=ZZlYlC6L",
      "image-large": "http://clients.view9.com.np/jwaremap/sites/default/files/styles/large/public/events/2250x3324-christmas-fair_0.png?itok=-vZhcEag",
      "image-providercategory_threex": "http://clients.view9.com.np/jwaremap/sites/default/files/styles/providercategory-threex/public/events/2250x3324-christmas-fair_0.png?itok=hwpr3WQd",
      "field_events_location": {
        "lat": "-3.416825167067",
        "lon": "36.657226562500"
      },
      "field_events_address": "TGT Grounds",
      "field_events_email": "info@arushafair.com",
      "field_events_contact": null,
      "field_events_website": "www.arushafair.com",
      "field_trip_advisor": null,
      "field_facebook": "https://web.facebook.com/events/681411865357050/",
      "field_twitter": null,
      "field_youtube": "https://www.youtube.com/watch?v=_Pvisfxe6CA",
      "field_info": "The ARUSHA COMMUNITY CHRISTMAS FAIR is the most anticipated holiday shopping and family fun event of the year -- hosting over 125 vendors and artisans from across East Africa and 5,000 visitors enjoying local talent, Arusha's best food and drink, lots of activities for the kids, and the chance to take home great prizes in the fantastic raffle and silent auction."
    }
  ],
  "author": {
    "name": "superadmin",
    "field_user_phone": "",
    "field_user_address": {
      "lat": "",
      "lon": "",
      "address": ""
    },
    "modified_date": "1475475942"
  },
  "total_items": 1,
  "last_update": "1475475942"
}

但是,它总是进入else块并抛出错误

错误域= NSCocoaErrorDomain代码= 3840“垃圾结束。” UserInfo = {NSDebugDescription =结尾处的垃圾。})

在打印错误说明时,它会返回 “无法读取数据,因为格式不正确。”

这是我的网址:

http://clients.view9.com.np/jwaremap/api/v1/content/events

这是所需的标题密钥:

[ “校验和”: “3389dae361af79b04c9c8e7057f60cc6”]

在使用jsonlint.com验证json结构时,它会将其验证为有效的json。在使用.responseString而不是.responseJSON时,我得到了结果。

我不知道我错过了哪里

有人请告诉我我错过了哪里..

1 个答案:

答案 0 :(得分:0)

检查您的网络服务响应,它不是有效的json,这就是您遇到此类错误的原因。

尝试在此处查看您的json响应: - http://jsonviewer.stack.hu/

有一个缺失的perenthesis' }'在邮递员json回复的最后。