-3

I am not getting what to do please help me the error is:

Type 'Any' has no subscript members

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var Lab: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        if let url = URL(string: "MY URL")
        {
            processData(url: url);
        }
    }


    private func processData(url: URL)
    {
        let task = URLSession.shared.dataTask(with: url) { (data, response, error) -> Void in
            if error != nil {
                print(error)
            } else {
                if let result = data
                {
                    do{
                        let json = try JSONSerialization.jsonObject(with: result, options:JSONSerialization.ReadingOptions.mutableContainers)

                        print(json)

                        if let name = json["bname"] as? String
                        {
                            print (name);
                        }                   }
                    catch let error as NSError
                    {
                        print("error is \(error)")
                    }
                }
        }
    }

  task.resume()
}
}

And My JSON is:

{
    "bank_detail" =     (
                {
            0 = 1;
            1 = www;
            2 = "PRAKHAR SONI";
            3 =123;
            4 = UDAIPUR;
            5 = ee;
            6 = 3190;
            7 = "2017-03-15";
            8 = "2017-03-15 13:31:39";
            accno = 123;
            bid = 1;
            bname = www;
            "branch_code" = 3190;
            "branch_name" = UDAIPUR;
            date = "2017-03-15";
            "date_time" = "2017-03-15 13:31:39";
            ifsc = www;
            uname = "PRAKHAR SONI";
        },
                {
            0 = 3;
            1 = "www ";
            2 = "Sureshkumar Soni";
            3 = "123 ";
            4 = "UDAIPUR ";
            5 = "www ";
            6 = "3190 ";
            7 = "2017-03-17";
            8 = "2017-03-17 12:24:24";
            accno = "www ";
            bid = 3;
            bname = "www ";
            "branch_code" = "3190 ";
            "branch_name" = "UDAIPUR ";
            date = "2017-03-17";
            "date_time" = "2017-03-17 12:24:24";
            ifsc = "www ";
            uname = "Sureshkumar Soni";
        }
    );
}
rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
  • 3
    That's normal. You never said that `json` can have subscript (meaning you can do `json["someKey"]`). It's by default a `Any` object. So tell the compiler it's a `[String:Any]` with `let json = try JSONSerialization.jsonObject(with: result, options:[]) as? [String: Any]`. Also you shouldn't need `mutableContainers` in Swift. – Larme Feb 19 '18 at 07:31
  • Btw, now that you've show your JSON `json["bname"]` will return nil because you didn't understood the structure of your JSON. – Larme Feb 19 '18 at 08:33

1 Answers1

1

Your problem is not casting the json to it's expected content like [String:Any] if it's a dictionary , or [String] is it's array of strings , You can try this

let json = try JSONSerialization.jsonObject(with: result, options:JSONSerialization.ReadingOptions.mutableContainers) as? [String:Any]
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87