将json从url解析为swift 2.0

时间:2015-09-29 08:07:59

标签: json swift

我一直试图为我的iOS应用程序检索我的json数据。我尝试了很多不同的解决方案,但这些都不适合我。所以这是我用来从url读取json并转换它的代码。

let url = NSURL(string: "http://www.blind3d.byethost7.com/service.php")!

func load() {
        do {
            let request = NSURLRequest(URL: url)
            let data = try NSURLConnection.sendSynchronousRequest(request, returningResponse: nil)
            self.handleData(data)
        }
        catch let error as NSError {
            print("wieso dont you do siss : \(NSURLRequest(URL: url))")
            self.handleError(error)
        }
    }

func handleError(error : NSError?) {
    print("wieso dont you do siss : \(NSURLRequest(URL: url))")
    NSLog("%@", "Error with loading from \(url): \(error)")
}

func handleData(data : NSData) {
    do {
        let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
        handleJSON(json)
    }
    catch let error as NSError {
        handleError(error)
    }
}

但不知怎的,这不能正常运行。执行此方法时,我总是收到此错误:NSJSONSerialization

Error with loading from http://www.blind3d.byethost7.com/service.php: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})

我想用于我的应用的json数据是here 谢谢你们的帮助

3 个答案:

答案 0 :(得分:2)

出现问题是因为data try00; length student article $20; infile datalines dlm=' '; input student $ article $; datalines; A pen B pencil V book A pen A inkbottle B pen B pencil B pencil V book Z marker A inkbottle V book V pen V book ; data try01; set try00; pos=_n_; run; proc sort data=try01 out=try02; by student pos article; run; proc sort data=try02 out=stud(keep=student) nodupkey; by student; run; data shell; length occurrence 8.; set try02; if _n_>0 then delete; run; %macro loopstudent(); data _null_; set stud end=eof; if eof then call symput("nstu",_n_); run; %do i=1 %to &nstu; data _null_; set stud; if _n_=&i then call symput("stud&i",student); run; data thisstu; set try02; where student="&&stud&i"; dummyart=lag(article); retain occurrence 0; if dummyart ne article then occurrence=occurrence+1; else occurrence=occurrence; drop dummyart; run; proc append base=shell data=thisstu; run; %end; proc sort data=shell out=final; by pos; run; %mend loopstudent; %loopstudent(); 变量中没有实际的JSON。我尝试了您的网络服务,这是您在data中返回的内容,以及所有其他错误的html标记:

data

完整回复:

"This site requires Javascript to work, please enable Javascript in your
browser or use a browser with Javascript support"

这似乎发生了,因为在您尝试解析的网页中注入了一些Javascript,可能是出于统计或其他一些原因。

要自行检查,请在致电<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("26049265c821fd7227c09955cbb61ebc");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";location.href="http://www.blind3d.byethost7.com/service.php?ckattempt=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>之前打印您的数据print(data)

答案 1 :(得分:1)

尝试删除\ r \ n或使用&#39; \&#39;喜欢

\\r\\n 

你很高兴。顺便说一下,使用json很快就像这样痛苦,SwiftyJSON是一个必要的库,如果你经常处理json。

答案 2 :(得分:0)

这是安装&#34; testCookie-nginx-module&#34;的结果。 它应该可以阻止你主机上的DDOS攻击

当您第一次访问您的网站时,它会向您发送此JS代码,您的浏览器应该处理该代码并设置一个特殊的cookie(其名称为_test)

只有将此cookie附加到您的IP上,您的浏览器才能看到原始内容(您的内容:html php json等)。

似乎唯一的方法 - 处理这个JS(使用AES,HEX和其他JS函数,获取正确的_test cookie并使用此cookie发送另一个请求)