2013-07-23 4 views
3

iOS에서 mapquest 지오 코드 json을 구문 분석하려고하지만 NSJSONSterilization이 null을 반환합니다. 온라인 json checkers로 확인한 결과 URL이 사실 json 인 것 같습니다. 여기 iOS의 MapQuest JSON에 대해 NSJSONSerialization이 null을 반환합니다.

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 

는 때문에 utf-8NULL을 반환? 미리 감사드립니다.

답변

6

webBrowser에 URL을 붙여 넣어 서버가 실제로 무엇을 반환하는지 확인하십시오. 이 JSON은 renderBatch();에 싸여

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

이이 같은 것입니다. JSON은 그렇게 파싱되지 않습니다.

요청 URL에 콜백을 요청했기 때문에 이러한 현상이 발생합니다. 요청 URL에서 콜백 매개 변수 (&callback=renderBatch)를 제거하고 NSJSONSerilization은 문제없이 JSON을 비 직렬화 할 수 있습니다.

+0

당신은 정확하고 좋은 답변입니다. 또한 [JSONLint] (http://jsonlint.com/) –

+0

덕분에 렌더링이 사라졌습니다. 새 JSON 브라우저에서 괜찮아 보이지만 NSJSONSterilization 여전히 null을 반환합니다. – Spenciefy

+0

저에게 잘 작동합니다 : http://i.imgur.com/l3ejVzt.png –

0

결과 문자열에서 renderbatch({});을 삭제 해보세요. 그리고 그것은 당신을 위해 일을 할 것입니다. 당신이 rederBatch에 결과를 얻고있다

-1

, 당신은 URL에서 그것을 제거해야 지금 같은 NSJSONSerilization를 사용하여 분석 할 수있는 다음과 같은 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 

를 사용합니다.

+0

나는 다른 답변에 주석으로, json renderbatch없이 이제 괜찮아 보이지만 NSJSONSterilization 여전히 null을 반환합니다. – Spenciefy

+0

수정 된 답변 확인 NSUTF8StringEncoding이 유효한 NSJSONReadingOptions 일 때부터 –

+1

으로 구문 분석을 시도 하시겠습니까? 유효한 옵션은'NSJSONReadingMutableContainers','NSJSONReadingMutableLeaves' 및'NSJSONReadingAllowFragments'입니다. –