1

I have a PHP file that queries a database and echos the following results [{"longitude":"-73.0247","latitude":"40.8066"}]. In my Xcode project, I am trying to figure out how to use JSON to parse the data and I am having bugs with my current code.

class coordinates {
    var longitude: String?
    var latitude: String?
}

override func viewDidLoad() {
    super.viewDidLoad()

    do {
        let data = try(NSData(contentsOfURL: NSURL(string: "http://thisiscensored.net/get.php")!, options: NSDataReadingOptions.DataReadingMappedIfSafe))

        let jsonDictionary = try(NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers))

        print(jsonDictionary)
    } catch let err {
        print(err)
    }

When I build and run, I get the following error - Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end.

Update - cURL Terminal Output
[{"longitude":"-73.0247","latitude":"40.8068"}]
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

  • Save the content of that URL into a file, and examine that with a binary editor to see if there is any unexpected characters at the end. – kennytm Jun 09 '16 at 17:18
  • I used phpMyAdmin to export my SQL query to a CVS file, then I used a program called Hex Fiend to examine it, the output was `"-73.0247";"40.8068"` Does that mean their are unexpected characters at the end? If so, what additional steps could we take to troubleshoot this? – AlexanderHart Jun 09 '16 at 18:05
  • That output is not JSON... – kennytm Jun 09 '16 at 18:15
  • I have no experience with JSON - so I ask that you please bear with me. When you say `Save the content of that URL into a file`, I assumed that we were talking about the `echoed` data from the PHP file. If I want to save the content, are we referring to the Swift file? My knowledge of JSON is very limited because I was following a very short tutorial I found on YouTube. – AlexanderHart Jun 09 '16 at 18:42
  • Yes I mean that PHP file (`curl -O http://thisiscensored.net/get.php`). If that is really the output then the PHP is not generating JSON at all, and perhaps it is a problem on the server (PHP) side. – kennytm Jun 09 '16 at 18:56
  • I think I'm gonna take the time and read up more on JSON. Would you recommend W3 Schools as a good place to start learning more about this? – AlexanderHart Jun 09 '16 at 19:15
  • Just make sure the PHP prints out exactly what you described, `[{"longitude":"-73.0247","latitude":"40.8066"}]`. See http://stackoverflow.com/questions/4064444/returning-json-from-a-php-script. And w3schools... [well ](http://meta.stackoverflow.com/questions/280478/why-not-w3schools-com). – kennytm Jun 09 '16 at 19:40
  • I took some time to learn JSON and I was able to get the server side working as intended. I updated my initial question with the Terminal output of `curl -O http://thisiscensored.net/get.php` – AlexanderHart Jun 10 '16 at 04:06

0 Answers0