vivek bhoraniya
vivek bhoraniya

Reputation: 1535

parsing JSON file

I have JSON file in response.

 (
    {
    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";
        };
    };
}
 )

To parse lat and long of location i have write this code. but when i am converting that value into float value it is giving me error like this.

Blockquote

  -[__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

My code is:

 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];

and in log string is print like this:

 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"

Here is JSON which I am getting in response.

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

Upvotes: 1

Views: 293

Answers (1)

Rob
Rob

Reputation: 437442

If you look carefully at your NSLog results, you'll see that your line:

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

is returning

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

Rather than what you wanted

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

Those parentheses are telling you that valueForKey is returning an NSArray, not a NSString. Two solutions, first, you could just grab the first object from that array:

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

Alternatively, you might to use objectForKey rather than valueForKey. Unfortunately, your code snippet does not make it clear what nested array of dictionary objects you logged at the top of your question (e.g. I don't see any reference to @"geometry"). But let's say it was results. If that was the case, you might do something like:

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

In your updated question, you provide a more complete NSLog of the resulting object you received from your JSON parser. For example, let's assume that the NSDictionary you logged was called jsonObject. You could then extract that latitude and longitude via:

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];

Upvotes: 2

Related Questions