2016-07-06 45 views
0

Error: Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character如何使用下面提到的代码在Swift中序列化JSON请求?

func FAQ(token: String){ 
    let urlString = BaseURL + "faq?token=" + token 
    request(.GET, urlString).responseJSON(options: NSJSONReadingOptions.AllowFragments){(json) -> Void in 
     if json.result.error != nil{ 
      NSNotificationCenter.defaultCenter().postNotificationName(NotifRequestError.FAQ.rawValue, object: json.result.error, userInfo: nil) 
      print(json.result.error) 
      return 
     } 

     let responseDict = NSMutableDictionary(dictionary: json.result.value as! NSDictionary) 
     print(responseDict) 
     NSNotificationCenter.defaultCenter().postNotificationName(NotifRequestSuccess.FAQ.rawValue, object: self, userInfo: ["data": responseDict]) 
    } 
} 
+0

您的JSON是无效 – Gruntcakes

+0

这是我已经张贴在这里它的原因,该代码工作对于其他apis,但我在几个API调用中遇到了这个问题。 –

+1

如果你知道json是无效的,那么你的标题完全不适合实际的问题。 – Gruntcakes

回答

-1

请试试这个

let data = NSKeyedArchiver.archivedDataWithRootObject(urlString) 

让newData = NSJSONSerialization.JSONObjectWithData(数据!,选项:NSJSONReadingOptions(),错误:无)作为? NSData let urlStringToPass = NSString(data:newData !,编码:NSUTF8StringEncoding)

+0

它是抛出异常 –

-1

请检查邮递员的回复。 json响应格式是否合适 Bcoz如果json格式不正确.allowfragment不起作用。 JSON字符串必须用引号括起来:(见http://www.json.org的细节): 从尝试Nsjsonreadingoption允许片段mutablecontainer