无法读取JSON文件:" JSON文本不以数组或对象开头"

时间:2015-12-16 04:28:05

标签: ios json swift

我正在解析远程JSON文件以读取并返回NSDictionary但是收到错误:

  

错误域= NSCocoaErrorDomain代码= 3840" JSON文本不是以数组或对象开头,而是选项允许未设置片段。" UserInfo = {NSDebugDescription = JSON文本不以数组或对象开头,并且选项允许未设置片段。}

以下是我用于解析JSON的Swift代码:

//---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"
}
] }

我已经阅读了这个问题,我的错误建议的主要问题是JSON文件的格式,这意味着错误状态无法找到JSON文档或JSON文档中的数组的有效开头。

对于重复的问题,我会理解投票,但我找不到适合我情况的任何东西。 简而言之,我想要读取远程JSON文件并将其打印出来。然后我会使用结果然后存储到CoreData模型中。我为后来找到的所有答案都没有针对swift 2.0进行更新

1 个答案:

答案 0 :(得分:-1)

对我来说看起来不错。如果正常工作,则附加JSON文件

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

我只使用了strign和' dataUsingEncoding'方法,但它也没有任何问题

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

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