2

I have recieved a response object (res) in swift from REST API. It is of type. __NSArrayM. It contains a JSON format string which I want to parse.

{ JsonResult = "[ { \"IsAuth\":\"true\" } ]"; }  

It is a long JSON String and I have shortened it for simplicity.

To parse a json, the object needs to be of type Dictionary but I can't cast the object of type __NSArrayM into it.

I searched a lot but can't figure out anyway to read this JSON string.

Additional: Whichever object I try to cast the response object. I get the error -

Could not cast value of type '__NSArrayM' (0x107e86c30) to 'NSData' (0x107e86168) or whichever data type I cast into.

ash007
  • 311
  • 4
  • 24
  • Don't try to replicate your Objective-C habits in Swift. For example, don't use NSArray/NSDictionary, use Swift collections instead, etc. – Eric Aya Dec 20 '16 at 11:02
  • Yes I know, but it doesn't work either ways. – ash007 Dec 20 '16 at 11:10
  • `To parse a json, the object needs to be of type NSDictionary` No. In JSON, the root object needs to be either an array or a dictionary. And do not confuse NSArray / Swift Array / JSON array. :) – Eric Aya Dec 20 '16 at 11:13
  • `but it doesn't work either ways` What doesn't work? An array is not a dictionary. You can't magically cast one to the other... – Eric Aya Dec 20 '16 at 11:13
  • It doesn't work work neither with NSDictionary not Dictionary. Though I correct myself. – ash007 Dec 20 '16 at 11:29
  • Eric I understand that I can't cast the JSON into any random type. Since my res object is of type __NSArrayM. I tried casting it into Dictionary but it gave error. That's what I am not able to figure out. res object contains a string which contains a JSON. I want to extract that JSON so that I can use it. Please help how can I do this. – ash007 Dec 20 '16 at 12:30
  • @AyushOjha Ask the operator of the Rest API why a single dictionary is sent in an **array**. That's also a hint. – vadian Dec 20 '16 at 12:40
  • Your question doesn't make sense. A REST API sends JSON strings, which you have to then deserialize into Cocoa objects, usually using the JSONSerialization class. In order to get the result of a REST API into an NSArray you must have already run some code. Put that code in your question. – Duncan C Dec 20 '16 at 13:06
  • It looks like what you have is a JSON string that contains a dictionary, and the value for the key JsonResult inside that dictionary is another JSON string, which is nuts. We need to see the code that got you to this point. – Duncan C Dec 20 '16 at 13:07

1 Answers1

2

Let's do this step by step.

You say you have an object named "res" which is of type __NSArrayM and which contains this thing:

{ JsonResult = "[ { \"IsAuth\":\"true\" } ]"; } 

It means that you already have converted the JSON to an object, namely an NSArray.

In this array that we don't see, this thing you're showing us is a dictionary (that we will name "dict") with its value being a String which itself represents another JSON object.

Let's get the value using the key:

if let value = dict["JsonResult"] as? String {
    print(value)
}

Now "value" is supposed to be "[ { \"IsAuth\":\"true\" } ]".

This is a String which represents JSON. To decode the JSON, we first have to make the string into data then we can decode:

if let data = value.data(using: .utf8) {
    if let content = try? JSONSerialization.jsonObject(with: data, options: []),
        let array = content as? [[String: Any]]
    {
        print(array)
    }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253