1
我有响应的JSON文件。解析JSON文件
(
{
location = {
lat = "23.0429226";
lng = "72.51406419999999";
};
"location_type" = APPROXIMATE;
viewport = {
northeast = {
lat = "23.0531899";
lng = "72.5300716";
};
southwest = {
lat = "23.0326545";
lng = "72.4980568";
};
};
}
)
要解析拉特和长的位置,我写了这段代码。但是当我将该值转换为浮点值时,它会给我这样的错误。
块引用
-[__NSArrayI floatValue]: unrecognized selector sent to instance 0xdd2d080
2013-11-21 19:31:15.602 App[3515:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI floatValue]: unrecognized selector sent to instance 0xdd2d080'
*** First throw call stack:
(
0 CoreFoundation 0x0328c5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0300f8b6 objc_exception_throw + 44
2 CoreFoundation 0x03329903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0327c90b ___forwarding___ + 1019
4 CoreFoundation 0x0327c4ee _CF_forwarding_prep_0 + 14
5 App 0x000885d8 __55-[WorkLocationViewController sendLatLongRequestAction:]_block_invoke + 744
6 App 0x0002901e -[ASIHTTPRequest reportFinished] + 222
7 libobjc.A.dylib 0x0302181f -[NSObject performSelector:withObject:] + 70
8 Foundation 0x02c64c18 __NSThreadPerformPerform + 285
9 CoreFoundation 0x032158af __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
10 CoreFoundation 0x0321523b __CFRunLoopDoSources0 + 235
11 CoreFoundation 0x0323230e __CFRunLoopRun + 910
12 CoreFoundation 0x03231b33 CFRunLoopRunSpecific + 467
13 CoreFoundation 0x0323194b CFRunLoopRunInMode + 123
14 GraphicsServices 0x0462b9d7 GSEventRunModal + 192
15 GraphicsServices 0x0462b7fe GSEventRun + 104
16 UIKit 0x01d8294b UIApplicationMain + 1225
17 App 0x000040fd main + 141
18 libdyld.dylib 0x037fb725 start + 0
19 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
我的代码是:
NSDictionary *geo=[res valueForKey:@"geometry"];
NSDictionary *loc=[geo valueForKey:@"location"];
NSLog(@"latitiude is%@",[loc valueForKey:@"lat"]);
NSLog(@"lon is %@",[loc valueForKey:@"lng"]);
//NSLog(@"float value is%f",[[loc valueForKey:@"lat"] floatValue]);
NSString *str=[loc valueForKey:@"lat"];
NSLog(@"string is %@",str);
NSString *str1=[loc valueForKey:@"lng"];
workCoordinate.latitude=[str floatValue];
workCoordinate.longitude=[str1 floatValue];
和日志字符串是打印这样的:
2013-11-21 19:31:15.599 App[3515:a0b] latitiude is( "23.0429226" ) 2013-11-21 19:31:15.599 App[3515:a0b] lon is ( "72.51406419999999" ) 2013-11-21 19:31:15.600 App[3515:a0b] string is ( "23.0429226"
这里是我得到的JSON响应。
{
results = (
{
"address_components" = (
{
"long_name" = "Judges Bunglow Police Chawky";
"short_name" = "Judges Bunglow Police Chawky";
types = (
establishment
);
},
{
"long_name" = "Satya Marg";
"short_name" = "Satya Marg";
types = (
route
);
},
{
"long_name" = Bodakdev;
"short_name" = Bodakdev;
types = (
sublocality,
political
);
},
{
"long_name" = Ahmedabad;
"short_name" = Ahmedabad;
types = (
locality,
political
);
},
{
"long_name" = Ahmedabad;
"short_name" = Ahmedabad;
types = (
"administrative_area_level_2",
political
);
},
{
"long_name" = GJ;
"short_name" = GJ;
types = (
"administrative_area_level_1",
political
);
},
{
"long_name" = India;
"short_name" = IN;
types = (
country,
political
);
},
{
"long_name" = 380015;
"short_name" = 380015;
types = (
"postal_code"
);
}
);
"formatted_address" = "Judges Bunglow Police Chawky, Satya Marg, Bodakdev, Ahmedabad, GJ 380015, India";
geometry = {
bounds = {
northeast = {
lat = "23.0369726";
lng = "72.51639879999999";
};
southwest = {
lat = "23.03688";
lng = "72.5162807";
};
};
location = {
lat = "23.0368868";
lng = "72.5163184";
};
"location_type" = APPROXIMATE;
viewport = {
northeast = {
lat = "23.0382752802915";
lng = "72.51768873029151";
};
southwest = {
lat = "23.0355773197085";
lng = "72.5149907697085";
};
};
};
"partial_match" = 1;
types = (
police,
establishment
);
}
);
status = OK;
}
我曾尝试第一种方法,但它不工作我得到空值...我有整个后JSON文件u能请看看... – vivek
我已经尝试过你的代码,但我得到错误使用未声明的标识符。 jsonObject – vivek
确定了你的观点' – vivek