したがって、構築しているアプリケーション用にダウンロードしたJSONオブジェクトでJSONSerializationを実行しようとすると、次のエラーが発生します。基本的には、JSONファイルを作成してSugarSyncに保存し、プログラムにダウンロードして使用するための直接ダウンロードリンクを取得しています。私自身のbootleg APIの一種。JSONファイルをダウンロードした後、Swift 3でJSONを解析するNSCocoaErrorDomain Code = 3840
URLを使用してファイルを文字列に変換すると、ファイルが出力されるため、ダウンロードがわかります。問題は、私はこのエラーを越えて得ることができないということです。
エラードメイン= NSCocoaErrorDomainコード= 3840「文字2の前後にオブジェクトの値の文字列キーがありません」
これは、ファイルの読み取り方法の一例です(スーパーシンプル!):
{"buddy": "pal",
"brother": "bear"}
私はNSDataの代わりのデータに変更しようとしたが、それは多くの問題を作成します。 JSONSerializationラインでNSDictionaryを実行しようとしましたが、それはより多くのエラーを投げかけました。私も.mutableContainersを試してみました。
以下は、SugarSyncファイルのURLを含めて、エラーコードを自分で再作成するためのコード全体です。私は間違っていることを理解していないし、誰かが助けることができれば、それはすばらしいだろう。
ありがとうございました!
// Create destination URL
let documentsURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
let destinationFileUrl = documentsURL.appendingPathComponent("downloadedFile.json")
// Create URL to the source file you want to download
let fileURL = URL(string: "https://www.sugarsync.com/pf/D7126167_796_275761334?directDownload=true")
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let request = URLRequest(url: fileURL!)
let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
// Success
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
print("Successfully downloaded. Status code: \(statusCode)")
}
do {
print("move files")
try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
let fileData: Data = try Data(contentsOf: destinationFileUrl, options: Data.ReadingOptions.mappedIfSafe)
print(fileData)
let myJson = try JSONSerialization.jsonObject(with: fileData, options: []) as? [[String: Any]]
print(myJson!)
do {
// This converts the file into String and is printed (doesn't get to this because of the error, but it works!)
let jsonText = try String(contentsOf: tempLocalUrl, encoding: String.Encoding.utf8)
print(jsonText)
} catch {
print("failed to print json file")
}
} catch (let writeError) {
print("Error creating a file \(destinationFileUrl) : \(writeError)")
}
} else {
print("Error took place while downloading file. Error description: %@", error?.localizedDescription ?? "unknown")
}
}
task.resume()
EDIT --------------------------- シリアライズは非常に敏感であるように見えます。
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity/100) * 90;"
}
}}
しかし、この1は動作しません:それは動作するように、このような何かを必要と
{“website”: {
“iPhone”: {
“blogURL”: ”string”,
“blogHead”: “string”,
“reportURL”: “string”,
“reportHead”: “string”
}
}}
注:このウェブサイトは、適切にJSONのくぼみを示していないが。 This is where I'm getting my JSON examples。
ありがとうございました!私はそれを試したと思うが、私は今日後で家に帰るときにそれを撃つだろう。今日は後でお知らせします。それが動作することを願って!再度、感謝します! – Bennybear
残念ながら、それは動作しませんでした。残念な。それがJSONドキュメントであるかどうかを確認するためにJSONドキュメントを少し変更しましたが、そうではありません。 – Bennybear