0

I'm trying to parse some JSON, this is what it's returning when I directly go to the URL:

[{"Password":"whatever1"}]

My code is able to receive the data correctly (when I debugged the variable "data" had the above JSON) however when trying to Parse it, it won't work. I think it might have to do with the square brackets, cause I've been parsing other JSONs without the square brackets and it works well.

Here is my code:

func SignIn (username: String, password: String, completion: @escaping (Bool)->())
{
    let url = URL(string: "http://<myIP>/API/SignIn.php?username=\(username)");

    let task = URLSession.shared.dataTask(with: url!)
    { (data, response, error) in
        if let data = data
        {
            do
            {
                // Convert the data to JSON
                let jsonSerialized = try JSONSerialization.jsonObject(with: data, options: []) as? [String : Any]

              //  print( jsonSerialized)
                if let json = jsonSerialized, let result = json["Password"]
                {
                    print(result)
                    if (String(describing: result) == password)
                    {
                        completion(true)
                    }
                    else
                    {
                        completion(false)
                    }
                    // TODO: password is wrong,
                    // TODO: username is wrong
                    // TODO: else if timeout
                }
            }
            catch let error as NSError {
                print(error.localizedDescription)
                completion(false)
            }
        }
        else if let error = error
        {
            print(error.localizedDescription)
            completion(false)
        }
    }
    task.resume()

}
Curtain Call LLC
  • 144
  • 1
  • 1
  • 11
  • 4
    `[String : Any]` is a dictionary. But your JSON is an array of dictionaries, not a dictionary. – Eric Aya Feb 16 '18 at 13:20
  • @Moritz oh ok, so I should just have String : Any instead of [String : Any] ? – Curtain Call LLC Feb 16 '18 at 13:23
  • No, you should have `[[String : Any]]`, an array of dictionaries. – Eric Aya Feb 16 '18 at 13:24
  • Be careful to not conflate JSON syntax with Swift syntax. The brackets do not represent the same thing! – Eric Aya Feb 16 '18 at 13:24
  • JSON dictionary: `{}`, JSON array: `[]`, Swift dictionary: `[String:Any]`, Swift array: `[]`. So, use a Swift array of dictionaries: `[[String : Any]]` because the JSON is `[{}]`. – Eric Aya Feb 16 '18 at 13:26
  • @Moritz ok thank you! I changed it to `[[String : Any]]`, but now it's screaming `Cannot subscript a value of type '[[String : Any]]' with an index of type 'String'` here: `let result = json["Password"]` – Curtain Call LLC Feb 16 '18 at 13:34
  • Of course. Think about it. You cannot access an array like you access a dictionary. First access the array, *then* access its content - the dictionary. – Eric Aya Feb 16 '18 at 13:35
  • 1
    Possible duplicate of [How to parse a JSON file in swift?](https://stackoverflow.com/questions/24013410/how-to-parse-a-json-file-in-swift) – Sharad Chauhan Feb 16 '18 at 13:41

1 Answers1

2

Rewrite code to :

let jsonSerialized = try JSONSerialization.jsonObject(with: data, options: []) as? [[String : Any]]

This is needed as your JSON response is an array of dictionaries, like the others have mentioned.

Access the result using:

let result = json.first["Password"]
SWAT
  • 1,107
  • 8
  • 19