2010-11-14 42 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