2010-11-14 74 views
2

我得到一個無效的JSON字符串,其中包含; (字符的任意猜測到底是怎麼回事無效touchJSON串

我的代碼:

-(void)getJSONFeed { 
    // Create the URL & Request  
NSURL *feedURL = [NSURL URLWithString:  
    @"http://maps.googleapis.com/maps/api/geocode/json?  address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true"];  
NSURLRequest *request = [NSURLRequest requestWithURL:feedURL]; 
// Example connection only. Add Timeouts, cachingPolicy in production 
[NSURLConnection connectionWithRequest:request delegate:self ]; 
// init the jsonData Property 
jsonData = [[NSMutableData data] retain]; 
} 

// NSURLConnection Delegate Methods. You would want to include more for error handling // 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSMutableData *)data { 
NSLog(@"Recieving Data..."); 
// Append the incomming data as it is received 
[jsonData appendData:data]; 
NSLog(@"%@",jsonData); 
} 

-(NSDictionary *)parseJSON:(NSMutableData *)data { 
NSLog(@"Parsing JSON");  
NSError *error = nil; 
NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&error]; 
return dictionary; 
} 

// Parse JSON results with TouchJSON. It converts it into a dictionary. 


-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 
NSLog(@"Fininshed Loading..."); 
NSDictionary * feedDictionary = [self parseJSON:jsonData]; 
NSLog(@"JSON as NSDictionary: %@", feedDictionary); 
} 

{ 
    results =  (
       { 
      "address_components" =    (
           { 
        "long_name" = 1600; 
        "short_name" = 1600; 
        types =      (
         "street_number" 
        ); 
       }, 
           { 
        "long_name" = "Amphitheatre Pkwy"; 
        "short_name" = "Amphitheatre Pkwy"; 
        types =      (
         route 
        ); 
       }, 
           { 
        "long_name" = "Mountain View"; 
        "short_name" = "Mountain View"; 
        types =      (
         locality, 
         political 
        ); 
       }, 
           { 
        "long_name" = "San Jose"; 
        "short_name" = "San Jose"; 
        types =      (
         "administrative_area_level_3", 
         political 
        ); 
       }, 
           { 
        "long_name" = "Santa Clara"; 
        "short_name" = "Santa Clara"; 
        types =      (
         "administrative_area_level_2", 
         political 
        ); 
       }, 
           { 
        "long_name" = California; 
        "short_name" = CA; 
        types =      (
         "administrative_area_level_1", 
         political 
        ); 
       }, 
           { 
        "long_name" = "United States"; 
        "short_name" = US; 
        types =      (
         country, 
         political 
        ); 
       }, 
           { 
        "long_name" = 94043; 
        "short_name" = 94043; 
        types =      (
         "postal_code" 
        ); 
       } 
      ); 
      "formatted_address" = "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA"; 
      geometry =    { 
       location =     { 
        lat = "37.422782"; 
        lng = "-122.085099"; 
       }; 
       "location_type" = ROOFTOP; 
       viewport =     { 
        northeast =      { 
         lat = "37.4259296"; 
         lng = "-122.0819514"; 
        }; 
        southwest =      { 
         lat = "37.4196344"; 
         lng = "-122.0882466"; 
        }; 
       }; 
      }; 
      types =    (
       "street_address" 
      ); 
     } 
    ); 
    status = OK; 
} 

UPDATE:

不知怎的,它解釋爲一個屬性列表中的格式似乎是相似的。原來NeXTSTEP的格式=;

回答

0

我不是100%肯定,問題是你做一個有效的HTTP連接,這使得谷歌從一個有意義的要求是什麼(如果你。刪除中間的六個空格,這幾乎肯定是代碼複製和粘貼到這裏的結果)。你累積結果。在給定的代碼中,您似乎泄漏了對象jsonData,但我認爲這與問題無關。

您使用的我沒有聽說過,但似乎在谷歌被普遍提及的CJSONDeserializer對象,可能是值得信賴的。它返回一個有效的NSDictionary。你打印字典,它有正確的結果。

就是這樣,當你打印字典到控制檯,它看起來並不等同於您收到的JSON的混亂?如果是這樣,那是因爲它不再有任何來自JSON和Cocoa的概念早於JSON標準,因此不用它來記錄日誌。

在任何情況下,feedDictionary是一個有效的字典。以下內容:

NSLog(@"%@", [feedDictionary objectForKey:@"status"]); 

會打印字符串'OK'。這:

NSArray *addressComponents = [feedDictionary objectForKey:@"address_components"]; 
for(NSDictionary *component in addressComponents) 
{ 
    NSLog(@"%@", [component objectForKey:@"long_name"]); 
} 

將打印字符串 '1600', '劇場PKWY', '山景', '聖何塞', '聖克拉拉', '加利福尼亞', '美國', '94043' 在該訂單。

如果你要打印的原始JSON到控制檯,你可能想是這樣的(假設的結果回來爲UTF8):

-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 
NSLog(@"Fininshed Loading..."); 

NSString *feedString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
NSLog(@"JSON was: %@", feedString); 
[feedString release]; 

/*NSDictionary * feedDictionary = [self parseJSON:jsonData]; 
NSLog(@"JSON as NSDictionary: %@", feedDictionary); */ 
} 

雖然那麼你仍然需要它來解析一本字典可以從中得到有意義的結果。

+0

謝謝湯米!你已經回答了我的問題。我對NSLog打印輸出感到困惑,但事實上,正如你指出的那樣,如果你使用NSUTF8StringEncoding,你會得到一個有效的json打印輸出。我只需要處理'狀態'和'結果'鍵和你的解決方案:NSArray * results = [feedDictionary objectForKey:@「results」]; NSArray * addressComponents = [[results objectAtIndex:0] objectForKey:@「address_components」];對於(地址組件中的NSDictionary *組件){ \t \t NSLog(@「%@」,[component objectForKey:@「long_name」]); \t} – Zsolt 2010-11-16 05:30:49