当我尝试显示一个String时,我的应用程序崩溃了:
NSLog(@"The start point is: %@",startPt);
这是startPt
值:39-49%20Avenue%20d'Ivry,%2075013%20Paris,%20France
。实际上它是从谷歌地图网络服务返回的。
有时,应用程序没有崩溃,但我在日志中得到了一些奇怪的东西:
The start point is: text/javascript
为什么我无法将字符串39-49%20Avenue%20d'Ivry,%2075013%20Paris,%20France
分配给NSString
变量?
修改
以下是JSON响应解析:
NSString *responseString=[request responseString];
NSDictionary *responseDict=[responseString JSONValue];
NSArray *resultsArray = [responseDict objectForKey:@"results"];
location=[[resultsArray valueForKey:@"formatted_address"] objectAtIndex:0];
location=[location stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(@"The location is : %@",location);//39-49%20Avenue%20d'Ivry,%2075013%20Paris,%20France
这是Google maps API返回的JSON响应:
{
"results" : [
{
"address_components" : [
{
"long_name" : "39-49",
"short_name" : "39-49",
"types" : [ "street_number" ]
},
{
"long_name" : "Avenue d'Ivry",
"short_name" : "Avenue d'Ivry",
"types" : [ "route" ]
},
{
"long_name" : "Quartier de la Gare",
"short_name" : "Quartier de la Gare",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "13th arrondissement of Paris",
"short_name" : "13th arrondissement of Paris",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "75",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Île-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
},
{
"long_name" : "75013",
"short_name" : "75013",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "39-49 Avenue d'Ivry, 75013 Paris, France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 48.82332110,
"lng" : 2.36638740
},
"southwest" : {
"lat" : 48.82296810,
"lng" : 2.36571090
}
},
"location" : {
"lat" : 48.82313960,
"lng" : 2.36602210
},
"location_type" : "RANGE_INTERPOLATED",
"viewport" : {
"northeast" : {
"lat" : 48.82449358029150,
"lng" : 2.367398130291502
},
"southwest" : {
"lat" : 48.82179561970850,
"lng" : 2.364700169708498
}
}
},
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "Quartier de la Gare",
"short_name" : "Quartier de la Gare",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "13th arrondissement of Paris",
"short_name" : "13th arrondissement of Paris",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "75",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Île-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Quartier de la Gare, Paris, France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 48.83810700000001,
"lng" : 2.3869610
},
"southwest" : {
"lat" : 48.8189240,
"lng" : 2.3556050
}
},
"location" : {
"lat" : 48.82807520,
"lng" : 2.36740680
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 48.83810700000001,
"lng" : 2.3869610
},
"southwest" : {
"lat" : 48.8189240,
"lng" : 2.3556050
}
}
},
"types" : [ "neighborhood", "political" ]
},
{
"address_components" : [
{
"long_name" : "13th arrondissement of Paris",
"short_name" : "13th arrondissement of Paris",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "75",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Île-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "13th arrondissement of Paris, Paris, France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 48.84496499999999,
"lng" : 2.3900530
},
"southwest" : {
"lat" : 48.8155730,
"lng" : 2.3411080
}
},
"location" : {
"lat" : 48.83222220,
"lng" : 2.35555560
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 48.84496499999999,
"lng" : 2.3900530
},
"southwest" : {
"lat" : 48.8155730,
"lng" : 2.3411080
}
}
},
"types" : [ "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "75013",
"short_name" : "75013",
"types" : [ "postal_code" ]
},
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "75",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Île-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "75013 Paris, France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 48.84493089999999,
"lng" : 2.39026440
},
"southwest" : {
"lat" : 48.81587960,
"lng" : 2.34112720
}
},
"location" : {
"lat" : 48.8307590,
"lng" : 2.3592040
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 48.84493089999999,
"lng" : 2.39026440
},
"southwest" : {
"lat" : 48.81587960,
"lng" : 2.34112720
}
}
},
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "75",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Île-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Paris, France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 48.9021450,
"lng" : 2.46992090
},
"southwest" : {
"lat" : 48.8155730,
"lng" : 2.2241990
}
},
"location" : {
"lat" : 48.8566140,
"lng" : 2.35222190
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 48.9021450,
"lng" : 2.46992090
},
"southwest" : {
"lat" : 48.8155730,
"lng" : 2.2241990
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Paris",
"short_name" : "75",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Île-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Paris, France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 48.9021450,
"lng" : 2.46992090
},
"southwest" : {
"lat" : 48.8155730,
"lng" : 2.2241990
}
},
"location" : {
"lat" : 48.87633370,
"lng" : 2.34612070
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 48.9021450,
"lng" : 2.46992090
},
"southwest" : {
"lat" : 48.8155730,
"lng" : 2.2241990
}
}
},
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "Île-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Île-de-France, France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 49.2415040,
"lng" : 3.5590070
},
"southwest" : {
"lat" : 48.1200810,
"lng" : 1.446170
}
},
"location" : {
"lat" : 48.84991980,
"lng" : 2.63704110
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 49.2415040,
"lng" : 3.5590070
},
"southwest" : {
"lat" : 48.1200810,
"lng" : 1.446170
}
}
},
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 51.08896180,
"lng" : 9.559956999999999
},
"southwest" : {
"lat" : 41.34232760,
"lng" : -5.1412280
}
},
"location" : {
"lat" : 46.2276380,
"lng" : 2.2137490
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 51.08896180,
"lng" : 9.559956999999999
},
"southwest" : {
"lat" : 41.34232760,
"lng" : -5.1412280
}
}
},
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
答案 0 :(得分:1)
我可以想到两种可能性:
(1)为什么假设字符串是ASCII?尝试不同的编码:
location = [location stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
也许Google正在使用unicode '而不是ASCII集中的unicode?如果您正在查看地址数据,那么您可以相当确定奇怪的字符将被放入那里!
和(2)(考虑到你已经在你的问题中发布了JSON但是值得考虑,这个更不可能)
当你第一次得到location
时,你没有检查它实际上是一个字符串(你没有检查'formatted_address'是否也是一个数组!)
如果返回的JSON是这样的(假设服务器中有错误,那该怎么办?):
{
...
"formatted_address" : [ { } ]
...
}
此处[[resultsArray valueForKey:@"formatted_address"] objectAtIndex:0];
可行,但不会返回NSString
。
尝试添加此类支票:
location=[[resultsArray valueForKey:@"formatted_address"] objectAtIndex:0];
if (NO == [location isKindOfClass:[NSString class]]) {
NSLog(@"Location wasn't a string (%@) : %@", [location class], location);
} else {
location=[location stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(@"The location is : %@",location);//39-49%20Avenue%20d'Ivry,%2075013%20Paris,%20France
}
答案 1 :(得分:0)
我的猜测是startPt不是NSString,而是char*
。如果是这种情况,从中创建NSString的最简单方法是使用[NSString stringWithFormat:@"%s", startPt]
。