如何使用下面提到的代码在Swift中序列化JSON请求?

时间:2016-07-06 04:46:55

标签: ios iphone json swift alamofire

  

错误:Domain = NSCocoaErrorDomain Code = 3840"周围的值无效   字符0。" UserInfo = {NSDebugDescription =周围的值无效   字符

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])
    }
}

2 个答案:

答案 0 :(得分:-1)

请试试这个

{if $cms->id==6}
{literal}
<script type="text/javascript" src="js/yourjsfile.js"></script>
{/literal}
{/if}

让newData = NSJSONSerialization.JSONObjectWithData(data!,options:NSJSONReadingOptions(),error:nil)为?的NSData  let urlStringToPass = NSString(data:newData!,encoding:NSUTF8StringEncoding)

答案 1 :(得分:-1)

请查看邮递员的回应。 json响应是否是正确的格式 Bcoz如果json的格式不正确.allowfragment将不起作用。 JSON字符串必须用引号括起来:(有关详细信息,请参阅http://www.json.org): 尝试从允许片段到mutablecontainer的Nsjsonreadingoption