3

我正在尝试在 iOS 中解析 mapquest 地理编码 json,但 NSJSONSterilization 返回 null。我检查了在线 json 检查器,看来 url 实际上是一个 json。

这是代码NSJSONSerialization

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


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

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

这是JSON为解析返回的 URL。

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

NULL是因为回来utf-8吗?提前致谢

4

3 回答 3

6

将 url 粘贴到您的 webBrowser 以查看服务器实际返回的内容。这将是这样的:

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

这是包裹在renderBatch(和中的 JSON );。这不是真正可解析的 JSON。

发生这种情况是因为您在请求 url 中请求回调。&callback=renderBatch从您的请求 url中删除回调参数 ( ),并且NSJSONSerilization可以毫无问题地反序列化 JSON。

于 2013-07-23T06:11:51.510 回答
0

尝试renderbatch({});从您的结果字符串中删除。这将为您做事。

于 2013-07-23T06:14:45.280 回答
-1

你在 rederBatch 中得到结果,你必须从 URL 中删除它,使用以下 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

现在您可以使用相同的 NSJSONSerilization 进行解析。

于 2013-07-23T06:16:29.183 回答