2013-11-21 176 views
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; 
    } 

回答

2

如果您在您的NSLog結果仔細一看,你會看到你的行:

NSString *str=[loc valueForKey:@"lat"]; 
NSLog(@"string is %@",str); 

正在恢復

 
2013-11-21 19:31:15.600 appname[3515:a0b] string is (
"23.0429226" 
) 

而不是你想要的東西

 
2013-11-21 19:31:15.600 appnane[3515:a0b] string is "23.0429226" 

那些pa房租告訴你valueForKey返回NSArray,而不是NSString。有兩種解決方案,首先,你可以只從陣列搶到的第一個對象:

NSArray *latArray=[loc valueForKey:@"lat"]; 
NSString *str=latArray[0] 
NSLog(@"string is %@",str); 

或者,您可能使用objectForKey而非valueForKey。不幸的是,你的代碼片段並沒有說清楚你在問題頂部登錄的字典對象的嵌套數組(例如,我沒有看到對@"geometry"的任何引用)。但讓我們說這是results。如果是這樣的話,你可以這樣做:

for (NSDictionary *dictionary in results) { 
    NSDictionary *location = dictionary[@"location"]; // or [dictionary objectForKey:@"location"]; 
    NSString *latString = location[@"lat"]; // or [location objectForKey:@"lat"]; 
    NSString *lngString = location[@"lng"]; // or [location objectForKey:@"lng"]; 

    CGFloat lat = [latString floatValue]; 
    CGFloat lng = [lngString floatValue]; 
} 

在你更新的問題,給您提供您從JSON解析器收到生成的對象更完整NSLog。例如,我們假設您登錄的NSDictionary被稱爲jsonObject。然後,您可以通過提取緯度和經度:

NSString *status   = jsonObject[@"status"]; 
NSArray *results   = jsonObject[@"results"]; 

NSDictionary *result  = results[0]; // let's grab the first result 

NSArray *addressComponents = result[@"address_components"]; 
NSString *formattedAddress = result[@"formatted_address"]; 
NSDictionary *geometry  = result[@"geometry"]; 
NSNumber *partialMatch  = result[@"partial_match"]; 
NSArray *types    = result[@"types"]; 

NSDictionary *location  = geometry[@"location"]; 
NSString *latitudeString = location[@"lat"]; 
NSString *longitudeString = location[@"lng"]; 
CGFloat latitude   = [latitudeString floatValue]; 
CGFloat longitude   = [longitudeString floatValue]; 
+0

我曾嘗試第一種方法,但它不工作我得到空值...我有整個後JSON文件u能請看看... – vivek

+0

我已經嘗試過你的代碼,但我得到錯誤使用未聲明的標識符。 jsonObject – vivek

+0

確定了你的觀點' – vivek