从计算机硬盘读取json文件时如何快速解决信号sigabrt错误?

时间:2019-01-22 09:09:10

标签: swift macos cocoa

我的计算机中有一个json文件。我正在尝试通过以下代码阅读该内容:

func ReadQuestion(){  
    let path = "/Users/bishnudas/Documents/ExamFiles/questions.json"  
    do {  
        let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)  
        if(data.count != 0){  

            let jsonResult = try JSONSerialization.jsonObject(with: data)  
            self.exampage.ExamQuestion = data  
            self.exampage.showWindow(nil)  
            self.window?.close()  
        }  
    } catch {  
        print(error)  
    }  
}  

读取该json文件后,我将数据发送到另一个窗口,并尝试像这样转换该数据:

func LoadQuestionPaper(){  
    do{  
        let QSJS = try JSONSerialization.jsonObject(with: ExamQuestion!, options:JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary  //Getting error signal sigabrt here        

    }catch {  
        print(error)  
    }  
}  

控制台中的错误消息:

  

无法将类型'__NSArrayM'(0x7fffa16f0fd0)的值强制转换为'NSDictionary'(0x7fffa16f32d0)。
  2019-01-21 13:39:50.214466 + 0530 Unox学生控制台[2071:66993]无法将类型'__NSArrayM'(0x7fffa16f0fd0)的值强制转换为'NSDictionary'(0x7fffa16f32d0)。
  QSJS的印刷说明:
  (NSDictionary)QSJS =

我的json文件结构:

[{
    "id": 1,
    "type": "sub",
    "text": "{\\rtf1\\ansi\\ansicpg1252\\uc1\\htmautsp\\deff2{\\fonttbl{\\f0\\fcharset0 Times New Roman;}{\\f2\\fcharset0 Segoe UI;}}{\\colortbl\\red0\\green0\\blue0;\\red255\\green255\\blue255;}\\loch\\hich\\dbch\\pard\\plain\\ltrpar\\itap0{\\lang1033\\fs18\\f2\\cf0 \\cf0\\ql{\\fs24\\f0\\b {\\lang2057\\ltrch Short notes: Concept based (20 marks)}\\li0\\ri0\\sa0\\sb0\\fi0\\qj\\par}\r\n{\\fs24\\f0\\b {\\ltrch }\\li0\\ri0\\sa0\\sb0\\fi0\\qj\\par}\r\n{\\f2 {\\ltrch }\\li0\\ri0\\sa0\\sb0\\fi0\\ql\\par}\r\n}\r\n}",
    "num_toans": 4,
    "total_marks": 20,
    "questions": [{
        "IsAnswered": null,
        "markedForReview": null,
        "id": 1,
        "marks": 5,
        "type": "etq",
        "q_text": "{\\rtf1\\ansi\\ansicpg1252\\uc1\\htmautsp\\deff2{\\fonttbl{\\f0\\fcharset0 Times New Roman;}{\\f2\\fcharset0 Segoe UI;}}{\\colortbl\\red0\\green0\\blue0;\\red255\\green255\\blue255;}\\loch\\hich\\dbch\\pard\\plain\\ltrpar\\itap0{\\lang1033\\fs18\\f2\\cf0 \\cf0\\ql{\\fs24\\f0 {\\lang2057\\ltrch Why will ABC unit cost figures be different from TCA unit cost figures}{\\lang2057\\ltrch \\rdblquote }{\\lang2057\\ltrch ?}\\li0\\ri0\\sa0\\sb0\\fi0\\ql\\par}\r\n}\r\n}",
        "attachments": null,
        "choices": null,
        "tfq_ans": null,
        "blank_count": 0,
        "fitb_ans": null,
        "mcq_index": -1,
        "mcq_text": null,
        "excel_ans_attachment": "0",
        "word_ans_attachment": "0",
        "essay_ans_attachment": null,
        "subSections": null
    },
    ...
    ]
}]

任何人都可以帮助我解决这个问题,或者可以建议我将数据转换为字典的正确方法吗?

0 个答案:

没有答案