2011-05-19 18 views
0

我最近有求助於從Google Maps API獲取我的郵編數據。在這個問題的最後,我已經收錄了我從Google收到的JSON響應字符串的大片段。有沒有更好的方法來解析這個JSON,而不是我已經使用?

在我的代碼深處,我得到了答案,我使用Jonathan Wight的TouchJSON庫進行解析。這是我的代碼

NSStringEncoding encoding; 

// Fetch data from web service. 
NSString *jsonString = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:nil]; 
// Process JSON data returned from web service. 
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding]; 
NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:nil]; 

if (jsonString == nil) 
{ 
// bah 
} else if (![self isCancelled]) {// Check to see if we have been cancelled. 

    if (dictionary) { 
     NSArray* results = [dictionary objectForKey:@"results"]; 
     NSDictionary* types = [results objectAtIndex:1]; 
     NSArray* address_components = [types objectForKey:@"address_components"]; 
     NSDictionary* postcodes = [address_components objectAtIndex:0]; 
     NSString* postcode = [postcodes objectForKey:@"long_name"]; 
    // do stuff with the postcode 
    } 
// 
} 

注意我是如何獲得的各類字典和郵政編碼使用特定的一個NSArray偏移字典。我的問題是這樣的,

如何解析JSON字符串(最好使用TouchJSON)而不必硬編碼這些數組偏移?

它讓我覺得Google地圖響應字符串可能會發生變化,而我的硬編碼方法在此時會停止工作。有沒有人在那裏使用類似,但更強大的方法,他們會願意分享在這裏?

......谷歌地圖API的樣本響應串...... '目標' 郵編被加粗顯示

{ 「地位」: 「OK」, 「結果」:[{ 「類型「:[ 」STREET_ADDRESS「], 」的formatted_address「: 」9克拉倫登PL,利明頓溫泉,沃裏克CV32 5,UK「, 」address_components「:[{ 」LONG_NAME「: 」9「, 」SHORT_NAME「: 「9」, 「types」:[「street_number」] },{ 「long_name」:「Clarendon Pl」, 「SHORT_NAME」: 「A452」, 「類型」:[ 「路線」] },{ 「LONG_NAME」: 「Leamington溫泉」, 「SHORT_NAME」: 「Leamington溫泉」, 「類型」:[「局部性 「 」政治「] },{ 」LONG_NAME「: 」沃裏克「, 」SHORT_NAME「: 」沃裏克「, 」類型「:[ 」administrative_area_level_3「, 」政治「] },{ 」 LONG_NAME 「: 「沃裏克郡」, 「簡稱的」: 「Warks」, 「類型」: 「administrative_area_level_2」, 「政治」] },{ 「LONG_NAME」: 「英格蘭」, 「簡稱的」: 「英格蘭」 , 「types」:[「administrative_area_level_1」,「political」] },{ 「long_name」:「英國」, 「short_name」:「GB」, 「types」:[「country」,「political」 ] },{ 「LONG_NAME」: 「CV32 5」, 「SHORT_NAME」: 「CV32 5」, 「類型」:[ 「postal_code_prefix」, 「POSTAL_CODE」] }], 「幾何」:{ 「位置」:{ 「LAT」:52.2919849, 「LNG」:-1.53​​99505 }, 「LOCATION_TYPE」: 「RANGE_INTERPOLATED」, 「視口」:{ 「西南」:{ 「LAT」:52.2888374, 「LNG」:-1.5431216 }, 「東北」:{ 「LAT」:52.2951326, 「LNG」:-1。5368264 }} , 「界限」:{ 「西南」:{ 「LAT」:52.2918320, 「LNG」:-1.53​​99760 }, 「東北」:{ 「LAT」:52.2921380, 「 LNG「:-1.53​​99720 } } }},{ 」類型「:[ 」POSTAL_CODE「], 」的formatted_address「: 」利明頓溫泉,沃裏克CV32 5LD,UK「, 」address_components「:[{ 「long_name」:「CV32 5LD」, 「short_name」:「CV32 5LD」, 「類型」: 「POSTAL_CODE」] },{ 「LONG_NAME」: 「利明頓溫泉」, 「簡稱的」: 「利明頓溫泉」, 「類型」: 「本地」, 「政治」] } ,.........其它更多的JSON如下

回答

2

我從來沒有用過TouchJSON,但在我看來,那是因爲你希望只有一個結果,你硬編碼的偏移量是有的,但谷歌地圖API足夠靈活以返回多個結果。事實上,如果你有objectAtIndex:1表明你正在使用第二個結果集,那麼你寫的代碼實際上依賴於至少有兩個結果。

即使我已經得到的細節有錯,我懷疑你真正想做的事,而不是NSDictionary* types = [results objectAtIndex:1];是更多的東西一樣:

NSDictionary* types = [results objectAtIndex:[results indexOfObjectPassingTest:yourPredicateHere]]; 

如果你不知道怎麼謂詞工作,你」我會首先閱讀Apple的Predicate Programming Guide,瞭解如何設置謂詞(我標記爲yourPredicateHere)。 http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/predicates.html

+0

對我來說,這看起來確實是一個更好的方法 - 現在挺起來了;我將使用代碼來實現它,如果一切順利,我會將其標記爲已接受 - 非常感謝! – Damo 2011-05-20 07:26:02

相關問題