Jamshed Alam Jamshed Alam - 2 months ago 84
Objective-C Question

iOS swift 3.0 local JSON parsing crash

I am using a local JSON in my swift projects. I need to extract the data in array. I am getting an unexpected crash.

My JSON is:

{
cueca = (
{
agentName = "Sant";
applicationName = "Get CDR Avaya";
body = "Detail of the message.";
imageType = 1;
messageType = 1;
requestTime = "2016-01-01 00:00:04";
sendTime = "2016-01-01 00:00:04";
subject = "Mensagem 05";
},
{
agentName = "Sant2";
applicationName = "Script Programming2";
body = "Detail of the message.";
imageType = 1;
messageType = 1;
requestTime = "2016-01-01 00:00:03";
sendTime = "2016-01-01 00:00:03";
subject = "Mensagem 04";
}
);
}


MY code is:

let file = Bundle.main.path(forResource: "ABC", ofType: "json")! as NSString
let url = URL(fileURLWithPath: file as String)
let data = try! Data(contentsOf: url)
let json = try! JSONSerialization.jsonObject(with: data)


My issue is:


fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 6." UserInfo={NSDebugDescription=No string key for value in object around character 6.}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.46.2/src/swift/stdlib/public/core/ErrorType.swift, line 178


I need to get all the object inside "cuaca" that i can use the array for further use.

Answer

Finally its a valid json data issue if you are getting json data from server or from anywhere else just try to rectify the faults while creating json nods, whereas in your data you have cueca object with array of 2 agent record.

Valid JSON example:

{
    "cueca": [{
        "agentName": "Sant",
        // data so on
    }, {
        "agentName": "Sant2",
        // data so on
    }]
}

Here dictionary cueca containing an [ ] array of record.

Comments