How to populate an array with information from JSON File and calculate distance?

后端 未结 1 466
走了就别回头了
走了就别回头了 2020-12-22 11:58

I have a JSON File here:

     {
      \"People\": [{
       \"A1\": \"New York\",
       \"B1\": \"ShoppingMall1\",
       \"C1\": \"43.0757\",
       \"D1\"         


        
相关标签:
1条回答
  • 2020-12-22 12:17

    I had been working on your question and this are my results,

    First of all I recommend you to use one JSON framework such as SwiftyJSON but I don't use any because I don't know if you want to, so

    first we need to load our json using this code

    let pathForPlist = NSBundle.mainBundle().pathForResource("JSON", ofType: "json")!
    let JSONData = NSData(contentsOfFile: pathForPlist)
    

    after we need to parse this data and convert to JSONObject

    let JSONObject = try NSJSONSerialization.JSONObjectWithData(JSONData!, options: NSJSONReadingOptions.MutableContainers) as! [String:AnyObject]
    

    and convert to properly Objects using an initializer from Dictionary, note that we use NSJSONReadingOptions.MutableContainers because our json is an array of dictionaries

    this is the full code, note that I define a class for your data type named ObjectShop to help with the calculation later

    import UIKit
    import MapKit
    
    class ObjectShop
    {
        var A1 = ""
        var B1 = ""
        var C1 = ""
        var D1 = ""
    
        init?(dict:[String:AnyObject])
        {
            A1 = dict["A1"] as! String
            B1 = dict["B1"] as! String
            C1 = dict["C1"] as! String
            D1 = dict["D1"] as! String
        }
    
        func getCoordenate2D() -> CLLocationCoordinate2D
        {
            return CLLocationCoordinate2D(latitude: Double(self.C1)!, longitude: Double(self.D1)!)
        }
    }
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            let pathForPlist = NSBundle.mainBundle().pathForResource("JSON", ofType: "json")!
            let JSONData = NSData(contentsOfFile: pathForPlist)
            do
            {
                var objects = [ObjectShop]()
                let JSONObject = try NSJSONSerialization.JSONObjectWithData(JSONData!, options: NSJSONReadingOptions.MutableContainers) as! [String:AnyObject]
                print(JSONObject)
                for dic in JSONObject["People"] as! [[String:AnyObject]] {
                    print(dic)
                    let objc = ObjectShop(dict: dic)
                    objects.append(objc!)
                }
    
                for object in objects {
                    print(object.getCoordenate2D())
                }
            }
            catch _
            {
    
            }
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    }
    

    I hope this helps you, let me know if you have any question

    0 讨论(0)
提交回复
热议问题