2015-12-16 162 views
2

我解析遠程JSON文件中讀取並返回一個NSDictionary,但我得到的錯誤「JSON文本沒有用數組或對象開始」:無法讀取JSON文件:

Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

下面是我的斯威夫特爲解析JSON代碼:

//---This is where I'm parsing the file--// 
getJSON("https://www.dropbox.com/s/sx57tx3pwwffbly/Destination.json?dl=0") 




func getJSON(urlToRequest: String) -> NSDictionary? { 

    let data = NSData(contentsOfURL: NSURL(string: urlToRequest)!)! 

    do { 
     if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary { 
      return jsonResult 
     } 
    } catch let error as NSError { 
     print("Error: \(error.description)") 
    } 

    return nil 
} 
下面

是我的JSON文件

{ "results": [ 
{ 
    "Country": "AUS", 
    "Name": "Byron Bay", 
    "createdAt": "2015-11-03T00:41:45.275Z", 
    "objectId": "2GcIpJRdhR", 
    "updatedAt": "2015-11-03T00:41:49.379Z" 
}, 
{ 
    "Country": "ITA", 
    "Name": "Florence", 
    "createdAt": "2015-11-03T00:38:46.581Z", 
    "objectId": "3HQMfF0Qlw", 
    "updatedAt": "2015-11-03T00:38:49.128Z" 
}, 
{ 
    "Country": "AUS", 
    "Name": "Adelaide", 
    "createdAt": "2015-11-03T00:41:30.147Z", 
    "objectId": "60wG6lyp2P", 
    "updatedAt": "2015-11-03T00:41:32.707Z" 
}, 
{ 
    "Country": "GRC", 
    "Name": "Santorini", 
    "createdAt": "2015-11-03T00:40:07.472Z", 
    "objectId": "8nVQ3RQK80", 
    "updatedAt": "2015-11-03T00:40:11.595Z" 
}, 
{ 
    "Country": "AUS", 
    "Name": "Hobart", 
    "createdAt": "2015-11-30T00:43:11.225Z", 
    "objectId": "ATggTTYcDl", 
    "updatedAt": "2015-11-30T00:43:17.625Z" 
}, 
{ 
    "Country": "AUS", 
    "Name": "Perth", 
    "createdAt": "2015-11-03T00:41:36.415Z", 
    "objectId": "CxQWRcJHA2", 
    "updatedAt": "2015-11-03T00:41:40.629Z" 
}, 
{ 
    "Country": "ITA", 
    "Name": "Venice", 
    "createdAt": "2015-11-03T00:38:52.993Z", 
    "objectId": "CzF8b6oH4I", 
    "updatedAt": "2015-11-03T00:38:55.851Z" 
}, 
{ 
    "Country": "Aus", 
    "Name": "Newcastle", 
    "createdAt": "2015-11-30T23:13:06.695Z", 
    "objectId": "EXbIGKcx0E", 
    "updatedAt": "2015-11-30T23:13:09.490Z" 
}, 
{ 
    "Country": "ITA", 
    "Name": "Pisa", 
    "createdAt": "2015-11-03T00:39:48.342Z", 
    "objectId": "HDwfPh3nC4", 
    "updatedAt": "2015-11-03T00:39:50.968Z" 
}, 
{ 
    "Country": "ESP", 
    "Name": "Barcelona", 
    "createdAt": "2015-11-03T00:35:35.777Z", 
    "objectId": "M2cVQGkoA9", 
    "updatedAt": "2015-11-03T00:35:49.540Z" 
}, 
{ 
    "Country": "ITA", 
    "Name": "Cinque Terre", 
    "createdAt": "2015-11-03T00:39:34.556Z", 
    "objectId": "NVabusFOzM", 
    "updatedAt": "2015-11-03T00:39:36.820Z" 
}, 
{ 
    "Country": "AUS", 
    "Name": "Sydney", 
    "createdAt": "2015-11-03T00:35:15.984Z", 
    "objectId": "PLqYdubhnB", 
    "updatedAt": "2015-11-03T00:35:23.837Z" 
}, 
{ 
    "Country": "GRC", 
    "Name": "Zakynthos", 
    "createdAt": "2015-11-03T00:40:24.280Z", 
    "objectId": "QgihbSfUo7", 
    "updatedAt": "2015-11-03T00:40:28.381Z" 
}, 
{ 
    "Country": "ITA", 
    "Name": "Rome", 
    "createdAt": "2015-11-03T00:38:12.670Z", 
    "objectId": "SEThamOY2N", 
    "updatedAt": "2015-11-03T00:38:15.502Z" 
}, 
{ 
    "Country": "ITA", 
    "Name": "Messina", 
    "createdAt": "2015-11-03T00:39:25.597Z", 
    "objectId": "XRb07EF1Tv", 
    "updatedAt": "2015-11-03T00:39:28.159Z" 
}, 
{ 
    "Country": "GRC", 
    "Name": "Athens", 
    "createdAt": "2015-11-03T00:38:21.915Z", 
    "objectId": "XS4gvYTJO4", 
    "updatedAt": "2015-11-03T00:38:42.682Z" 
}, 
{ 
    "Country": "AUS", 
    "Name": "Darwin", 
    "createdAt": "2015-11-30T23:13:39.088Z", 
    "objectId": "Xu0J2tbyfi", 
    "updatedAt": "2015-11-30T23:13:41.076Z" 
}, 
{ 
    "Country": "AUS", 
    "Name": "Sunshine Coast", 
    "createdAt": "2015-11-30T00:43:47.810Z", 
    "objectId": "XuUX7eYO2R", 
    "updatedAt": "2015-11-30T00:43:50.529Z" 
}, 
{ 
    "Country": "ITA", 
    "Name": "Milan", 
    "createdAt": "2015-11-03T00:38:02.137Z", 
    "objectId": "YeqqW7IBwR", 
    "updatedAt": "2015-11-03T00:38:05.802Z" 
}, 
{ 
    "Country": "AUS", 
    "Name": "Gold Coast", 
    "createdAt": "2015-11-25T22:52:58.169Z", 
    "objectId": "cuVrgPuLxy", 
    "updatedAt": "2015-11-30T00:43:21.778Z" 
}, 
{ 
    "Country": "AUS", 
    "Name": "Cairns", 
    "createdAt": "2015-11-30T23:12:49.327Z", 
    "objectId": "cxf8wcvsX6", 
    "updatedAt": "2015-11-30T23:12:53.120Z" 
}, 
{ 
    "Country": "ESP", 
    "Name": "Madrid", 
    "createdAt": "2015-11-03T00:40:42.536Z", 
    "objectId": "fuk8y4owDC", 
    "updatedAt": "2015-11-03T00:40:45.618Z" 
}, 
{ 
    "Country": "GRC", 
    "Name": "Mykonos", 
    "createdAt": "2015-11-03T00:39:57.422Z", 
    "objectId": "gbXvwPpS41", 
    "updatedAt": "2015-11-03T00:40:02.912Z" 
}, 
{ 
    "Country": "GRC", 
    "Name": "Ios", 
    "createdAt": "2015-11-03T00:40:14.962Z", 
    "objectId": "hiWu6gKdJ0", 
    "updatedAt": "2015-11-03T00:40:18.221Z" 
}, 
{ 
    "Country": "ESP", 
    "Name": "Costa Brava", 
    "createdAt": "2015-11-03T00:37:50.021Z", 
    "objectId": "kg9CrSzxM3", 
    "updatedAt": "2015-11-03T00:37:54.461Z" 
}, 
{ 
    "Country": "AUS", 
    "Name": "Canberra", 
    "createdAt": "2015-11-25T22:53:28.205Z", 
    "objectId": "s0EbEsucbN", 
    "updatedAt": "2015-11-30T00:43:19.336Z" 
}, 
{ 
    "Country": "AUS", 
    "Name": "Terrigal", 
    "createdAt": "2015-11-03T00:41:54.467Z", 
    "objectId": "uvyotYvZjw", 
    "updatedAt": "2015-11-03T00:42:04.227Z" 
}, 
{ 
    "Country": "AUS", 
    "Name": "Brisbane", 
    "createdAt": "2015-11-03T00:41:12.373Z", 
    "objectId": "w8zcIGffqI", 
    "updatedAt": "2015-11-03T00:41:18.939Z" 
}, 
{ 
    "Country": "AUS", 
    "Name": "Melbourne", 
    "createdAt": "2015-11-03T00:41:22.251Z", 
    "objectId": "zQH8uI9RkM", 
    "updatedAt": "2015-11-03T00:41:26.004Z" 
} 
] } 

我已經讀U p在這個問題上,我的錯誤提示的主要問題是JSON文件的格式,這意味着錯誤狀態是無法找到JSON文檔或JSON文檔中的數組的有效開始。

將瞭解重複問題的投票,但我沒有找到適合我的情況的任何東西。 總之,我想讀取遠程JSON文件並打印出來。然後我會使用結果存儲到CoreData模型中。我發現後來的所有答案都沒有更新爲迅速2.0

+0

有人(刪除答案)提到,應刪除最外面的'{}'。然而,這似乎並不是W3C推薦的內容:http://www.w3schools.com/json/json_syntax.asp –

+0

@NicolasMiari回答是錯誤的,最外層的{}}實際上是必要的。 – Kevin

+1

我的2分診斷:顯示的文件是* not *正在分析的文本。 – user2864740

回答

0

它看起來對我很好。 JSON文件,如果連接正常工作

if let a = try? NSJSONSerialization.JSONObjectWithData(json.dataUsingEncoding(NSUTF8StringEncoding)!, options: .AllowFragments) { 
    print(a) 
} 

我用剛剛strign和「dataUsingEncoding」方法,但它shoudn't是任何這方面的問題太多

if let data = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("File", ofType: "txt")!) { 

     if let a = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) { 
      print(a) 
     } 
    }