2

我得到一个无效的 JSON 字符串,其中包含 ; (字符。猜猜发生了什么?

我的代码:

-(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;
}

更新:

它以某种方式将其解释为属性列表。该格式似乎类似于带有 = 的原始 NeXTSTEP 格式;

4

1 回答 1

0

我不是 100% 确定问题是什么。你做了一个有效的 HTTP 连接,这会从 Google 发出一个有意义的请求(如果你删除了中间的六个空格,这几乎肯定是代码复制并粘贴到这里的结果)。你积累结果。在给定的代码中,您似乎泄漏了对象 jsonData,但我认为这与问题无关。

您使用 CJSONDeserializer 对象,我没有听说过,但似乎在 Google 上经常提到,所以可能是值得信赖的。它返回一个有效的 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”、“Amphitheatre Pkwy”、“Mountain View”、“San Jose”、“Santa Clara”、“California”、“United States”、“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);  */
}

尽管如此,您仍然需要将其解析为字典以从中获得有意义的结果。

于 2010-11-15T01:46:09.210 回答