Getting error signal sigabrt while converting type data to NsDictionary.

Hello there, i have a json file in my computer. I am trying to read that by this code:

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)
        }
    }

After reading that json file i am sending the data to another window and trying to convert that data like this:

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

Error message in console:

Could not cast value of type '__NSArrayM' (0x7fffa16f0fd0) to 'NSDictionary' (0x7fffa16f32d0).
2019-01-21 13:39:50.214466+0530 Unox Student Console[2071:66993] Could not cast value of type '__NSArrayM' (0x7fffa16f0fd0) to 'NSDictionary' (0x7fffa16f32d0).
Printing description of QSJS:
(NSDictionary) QSJS = <variable not available>

Can anyone help me to solve this, or can suggest me the right way to convert that data into dictionary please?

Replies

What is the format of your JSON data (ExamQuestion) ? If it is a dictionary, I would try optional:


let QSJS = try JSONSerialization.jsonObject(with: ExamQuestion!, options:JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary

if let qsjs = QSJS  {   // note: var should start with lowercase

}

But isn't it an Array ?


Could you post ExamQuestion ? // Here also, should name it examQuestion

Here is the format of ExamQuestion:

[
{
"id" : 1,
"type": "sub"
"text": "{RTF Text content}"
"num": 4,
"questions": [{
               "IsAns": null,
               .......
               .......
              }, 
               ....
              ]
}]

Actually i want to bind that data into an NSOutlineView, so that i want to convert that into a dictionary. Is there any other way to bind that data to an NSOutlineView in the UI of mac os application ? Can you suggest me please.

What do you get if you print:


let QSJS = try JSONSerialization.jsonObject(with: ExamQuestion!, options:JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary

if let qsjs = QSJS  {   
      print(qsjs)
}

Nothing is printing, the "qsjs" is showing nil.

The outermost symbol of your JSON is `[`, which indicates it is a JSON array, not JSON object.


You cannot convert JSON array into `NSDictionary`.


func loadQuestionPaper() {
    do {
        if let qsjs = try JSONSerialization.jsonObject(with: examQuestion!) as? [[String: Any]] {
            print(qsjs)
        } else {
            print("examQuestion cannot be deserialized as an Array")
        }
    } catch {
        print(error)
    }
}


Some other things...

- In almost all cases, `JSONSerialization.ReadingOptions.mutableContainers` has no meaning in Swift.

(Mutability is represented by `var`, not by an option.)

- Many Swift programmers follow one simple convention: Only type names start with capital letter. I recommend you to follow it.