gpt4 book ai didi

ios - 无法读取 JSON 文件 : "JSON text did not start with array or object"

转载 作者:搜寻专家 更新时间:2023-11-01 05:44:12 25 4
gpt4 key购买 nike

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

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 的 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 更新

最佳答案

我觉得还行。如果工作正常则附加 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)
}
}

关于ios - 无法读取 JSON 文件 : "JSON text did not start with array or object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34303937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com