3

I am trying to parse a mapquest geocode json in iOS, but NSJSONSterilization returns null. I checked with online json checkers and it appears that the url is in fact a json.

Here is the code for NSJSONSerialization

 if(geocodeResponseData)
 {
     NSLog(@"there is response data");
     //this is logged.
 }


 NSDictionary *mapQuestReponse =  [NSJSONSerialization
                                     JSONObjectWithData:geocodeResponseData
                                     options:kNilOptions
                                     error:&error];

 NSLog(@"mapquestreponse %@", mapQuestReponse);

This is the JSON URL that is returned for parsing.

http://www.mapquestapi.com/geocoding/v1/batch?key=API----KEY----HIDDEN----&callback=renderBatch&outFormat=json&location=14443%20C%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14510%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14550%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14515%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=1480%20S%20De%20Anza%20Blvd,San%20Jose,%20CA%2095129&location=1600%20S%20De%20Anza%20Blvd,San%20Jose,%20CA%2095129&location=18486%20Prospect%20Rd,San%20Jose,%20CA%2095070&location=14572%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=5210%20Prospect%20Rd,San%20Jose,%20CA%2095129&location=1600%20S%20De%20Anza%20Blvd,San%20Jose,%20CA%2095106&location=14480%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=1818%20Saratoga%20Ave,San%20Jose,%20CA%2095129&location=18562%20Prospect%20Rd,Saratoga,%20CA%2095070&location=14560%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14420%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=1075%20S%20De%20Anza%20Blvd,Cupertino,%20CA%2095129&location=18802%20Cox%20Ave,Saratoga,%20CA%2095070&location=6154%20Bollinger%20Rd,San%20Jose,%20CA%2095129&location=14555%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14000%20Fruitvale%20Ave,Saratoga,%20CA%2095070

Is it returning NULL because of utf-8? Thanks in advance

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
Spenciefy
  • 892
  • 11
  • 33

3 Answers3

6

Paste the url in your webBrowser to see what the server actually returns. This will be something like this:

renderBatch(
    {"results":[{"locations":[{"latLng":{"lng":-122.032921,"lat":37.258389},"adminArea4":"Santa Clara" ........ }
);

This is JSON wrapped in renderBatch( and );. That's not really parsable JSON.

This happens because you request a callback in your request url. Remove the callback parameter (&callback=renderBatch) from your request url and NSJSONSerilization can deserialize the JSON without problems.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • thanks, the renderbatch is gone. the new JSON looks fine in browser, but NSJSONSterilization still returns null. – Spenciefy Jul 23 '13 at 06:17
0

try removing renderbatch({}); from your result string. and that will do the things for you.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
Armaan Stranger
  • 3,140
  • 1
  • 14
  • 24
-1

you are getting result in rederBatch, you have to remove that from the URL, use the following URL

http://www.mapquestapi.com/geocoding/v1/batch?key=Fmjtd%7Cluub206tl1%2Crg%3Do5-9ubah0&outFormat=json&location=14443%20C%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14510%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14550%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14515%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=1480%20S%20De%20Anza%20Blvd,San%20Jose,%20CA%2095129&location=1600%20S%20De%20Anza%20Blvd,San%20Jose,%20CA%2095129&location=18486%20Prospect%20Rd,San%20Jose,%20CA%2095070&location=14572%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=5210%20Prospect%20Rd,San%20Jose,%20CA%2095129&location=1600%20S%20De%20Anza%20Blvd,San%20Jose,%20CA%2095106&location=14480%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=1818%20Saratoga%20Ave,San%20Jose,%20CA%2095129&location=18562%20Prospect%20Rd,Saratoga,%20CA%2095070&location=14560%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14420%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=1075%20S%20De%20Anza%20Blvd,Cupertino,%20CA%2095129&location=18802%20Cox%20Ave,Saratoga,%20CA%2095070&location=6154%20Bollinger%20Rd,San%20Jose,%20CA%2095129&location=14555%20Big%20Basin%20Way,Saratoga,%20CA%2095070&location=14000%20Fruitvale%20Ave,Saratoga,%20CA%2095070

now you can parse using the same NSJSONSerilization.

Manish Agrawal
  • 10,958
  • 6
  • 44
  • 76
  • just as i commented on the other answer, the json looks fine now without renderbatch, but NSJSONSterilization still returns null. – Spenciefy Jul 23 '13 at 06:18
  • check the edited answer try parsing with NSUTF8StringEncoding – Manish Agrawal Jul 23 '13 at 06:22
  • 1
    since when is NSUTF8StringEncoding a valid `NSJSONReadingOptions`? The only valid options are `NSJSONReadingMutableContainers`, `NSJSONReadingMutableLeaves` and `NSJSONReadingAllowFragments` – Matthias Bauch Jul 23 '13 at 06:23