2

I'm trying to make an app for iPhone, which should consume webservice written in nodejs + MongoDB. The application is made in Swift, but now I have a problem, I have not managed correctly parse the data.

Currently I have a code like this:

    var endpoint = NSURL(string: self.url + "?latitud=" + self.latitude + "&longitud=" + self.longitude)
    var data = NSData(contentsOfURL: endpoint!)

    var error: NSError? = nil

    if let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary {
        for place in json {
            var name = place["obj"]["name"]
            var coords = place["obj"]["coords"]

            var annotation = MKPointAnnotation()
            annotation.title = name as? String
            annotation.coordinate = coords as? CLLocationCoordinate2D
            map.addAnnotation(annotation)
        }

    }

Unfortunately it does not work :(

The response from the webservice is similar to this:

[
    {
        "dis": 1.22,
        "obj": {
            "name": "Some name",
            "coords": [
                -97.1228,
                17.4049
            ],
            "phones": [
                "555 555 55555",
                "444 444 44444"
            ],
            "address": {
                "street": "Some Street",
                "zip": "00000"
            }
        }
    },
    {
        "dis": 2.03,
        "obj": {
            "name": "Othe name",
            "coords": [
                -97.0910
                17.7099
            ],
            "phones": [
                "777 777 7777"
            ],
            "address": {
                "street": "Other street",
                "zip": "11111"
            }
        }
    }
]

That's what I'm doing wrong? Is there a more elegant (and especially efficient) way to make requests to the API RESTful?

Rodrigo Moreno
  • 629
  • 9
  • 24

3 Answers3

2

Your rest API returns an array of NSDictionary, if you replace

if let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary {
with
if let boardsDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as? Array<NSDictionary> {
You should get your data as an array. This will let you access the data

milo526
  • 5,012
  • 5
  • 41
  • 60
1

The response data from the web service is a JSONArray, you should convert it into NSArray but not NSDictionary.

    if let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary {

I am not familiar with swfit, but seem like you should change this line.

If you have any problem with RESTful API call from client, you may try this http://unirest.io/

Unirest is a pretty good lightweight HTTP client for making RESTful API call and paring response with JSON.

littlet
  • 207
  • 1
  • 7
  • You are quite right, changing NSArray NSDictionary for me was better. That of Unirest is great, thanks for that recommendation – Rodrigo Moreno Mar 11 '15 at 07:09
0

I don't know about Swift, but I think you forgot quotes in your code...

var coords = place["obj"]["coords]

Maybe:

var coords = place["obj"]["coords"]