我收到一个无效的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格式相似=;
答案 0 :(得分: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); */
}
虽然那时你仍然需要将它解析为字典以从中获得有意义的结果。