0

I am developing an application which reads the names and the coordinations of shopping malls. Then it compares these to the user's current location, so that the user can see only the nearest shopping malls around him. For that reason I was thinking to get all of the names and locations into 2 arrays and compare them with the user's current location. Then I will prepare them for segue and send them to my TableViewController where I will display everyone of them in different cell.

I am new to Swift and I will be glad if someone can help me how to do that.

My JSON File is:

{
    "People": {
        "Person0": {
            "A1": "New York",
            "B1": "ShoppingMall1",
            "C1": "43.0757",
            "D1": "23.6172"
        },

        "Person1": {
            "A1": "London",
            "B1": "ShoppingMall2",
            "C1": "44.0757",
            "D1": "24.6172"
        },
        "Person2": {
            "A1": "Paris",
            "B1": "ShoppingMall3",
            "C1": "45.0757",
            "D1": "25.6172"
        },
        "Person3": {
            "A1": "Bern",
            "B1": "ShoppingMall4",
            "C1": "41.0757",
            "D1": "21.6172"
        },
        "Person4": {
            "A1": "Sofia",
            "B1": "ShoppingMall5",
            "C1": "46.0757",
            "D1": "26.6172"

        }
    }
}
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
Ivan Sosev
  • 33
  • 1
  • 6

1 Answers1

-1

Without third party libraries:

Swift 3:

func convertStringToDictionary(text: String) -> [String:AnyObject]? {
    if let data = text.data(using: String.Encoding.utf8) {
        do {
            return try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject]
        } catch let error as NSError {
            print(error)
        }
    }
    return nil
}

Swift 2:

func convertStringToDictionary(text: String) -> [String:AnyObject]? {
    if let data = text.dataUsingEncoding(NSUTF8StringEncoding) {
        do {
            return try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject]
        } catch let error as NSError {
            print(error)
        }
    }
    return nil 
}

and then you have a dictionary and you can have access to your peoples like this:

let dict = convertStringToDictionary(jsonText)
print(dict["People"])

With your json you cannot have an array since you should change the json like this:

{
    "People": [{
            "A1": "New York",
            "B1": "ShoppingMall1",
            "C1": "43.0757",
            "D1": "23.6172"
        },
        {
            "A1": "London",
            "B1": "ShoppingMall2",
            "C1": "44.0757",
            "D1": "24.6172"
        }, {
            "A1": "Paris",
            "B1": "ShoppingMall3",
            "C1": "45.0757",
            "D1": "25.6172"
        }, {
            "A1": "Bern",
            "B1": "ShoppingMall4",
            "C1": "41.0757",
            "D1": "21.6172"
        }, {
            "A1": "Sofia",
            "B1": "ShoppingMall5",
            "C1": "46.0757",
            "D1": "26.6172"

        }
    ]
}
Community
  • 1
  • 1
Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49
  • thank you very much forthe answer. One question: Where do you write the name of the JSON File so that the app knows from which JSON File to get the data – Ivan Sosev Aug 19 '16 at 07:21
  • It depends if your json is in the app bundle or downloaded from a server – Marco Santarossa Aug 19 '16 at 07:24
  • it is from a URL link – Ivan Sosev Aug 19 '16 at 07:27
  • @IvanSosev read this guide: http://www.learnswiftonline.com/mini-tutorials/how-to-download-and-read-json/ – Marco Santarossa Aug 19 '16 at 07:29
  • Thank you I can do that,but I still can't get what exactly is savevd into the dictionary. I have to compare the locations and if I compare the whole dictionary I will compare also names which are saved.I do not want to comapare them I need only C1 and D1 from the JSON File and the names A1 and B1 have to be displayed in another dictionary – Ivan Sosev Aug 19 '16 at 07:32
  • In the dictionary you have to do: dict["People"]["Person0"]["C1"] and dict["People"]["Person0"]["D1"] – Marco Santarossa Aug 19 '16 at 07:35
  • but in this case I have to write ["People"]["Person0"]["C1"],["People"]["Person0"]["D1"] and the the same for ["Person1"]....["Person2"].... I do not know how many times I have to write Person. I need to get once all of Person [C1 and D1]so that I can compare them with the users current location – Ivan Sosev Aug 19 '16 at 07:41
  • change the json like my proposal if possible so you remove `Person0` key and you have an array, otherwise you can use `for (key, value) in dict["People"]` – Marco Santarossa Aug 19 '16 at 07:48
  • Do you have a skype or something else where I can explain you – Ivan Sosev Aug 19 '16 at 07:51
  • https://stackoverflow.com/a/30480777/2227743 – Eric Aya Mar 13 '21 at 10:36