2013-02-11 25 views
0

我有一些問題需要獲取JSON文件中的特定鍵。 實際上,我正在使用MapView和JSON文件以及一些餐館的座標。獲取特定的關鍵JSON解析器IOS

我可以顯示我的JSON文件的內容,但是我無法在關鍵字lat(緯度)和lng(經度)獲得de內容。

這裏我的JSON文件:

{ 
    "html_attributions" : [ 
     "Adresses fournies par \u003ca href=\"http://www.yellowpages.com.au/\"\u003eYellow Pages\u003c/a\u003e" 
    ], 
    "next_page_token" : "ClRNAAAAtyfgXsHo6S4iCel9hSfwlphKSXVDh6Nb-PyQfYSGmGhSygljtG9GpZRL8Yfpka-ijz3WvES9b7Fil_PBX4lCfdl2agVIuIACCkAgeS05M8gSEK7qCd3HTQZFLBCRNcqhzkwaFIONRXOWfLV7IG6WYdQvu9mLTpxL", 
    "results" : [ 
     { 
     "geometry" : { 
      "location" : { 
       "lat" : -33.8681990, 
       "lng" : 151.1945850 
      } 
     }, 
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png", 
     "id" : "edd23ebe6cd17dcbd9139e2643324c9195a9e3e3", 
     "name" : "The Star", 
     "opening_hours" : { 
      "open_now" : true 
     }, 
     "photos" : [ 
      { 
       "height" : 1224, 
       "html_attributions" : [ "Utilisateur Google" ], 
       "photo_reference" : "CoQBfgAAAAecfnYhSZSQipHCYDPgsfLe8rkSAVmM6D6yQg7OKHviC_JvESqcgwFEQPITibnD-BrbNMQ5AvH81qJ99F5G6WmfWn3csIUOeXirwxUuqOUz7eWimBvEcPCaWVcL3E9NJCGQY3T12ElETuqZosJ2NV4igwxvNmmuqIzCOs7sRzpNEhDYfNl7hR5jKzdLoI0MW2WUGhTHmX-XTNrMJIJ0uATulmdpFGWg_Q", 
       "width" : 1632 
      } 
     ], 
     "rating" : 4.20, 
     "reference" : "CnRlAAAA4XhqmCyMUjmvRIgn-Brf4uubjgcISREb1m7SR4HPlkYauFAKYTnbixBAKjbJE9gzZ7Sk_PaBT7qF5GWynfqIXv9_mgZqgQTVn6fA5Xy0QESzs2Q70o49T7G2E1wrrE4frK13o1_xx6iqxvPsYLSCjBIQX5NR5WgFPscjobiWYalyYxoUIy68fs-7HekAPhpmwRr2FraDi3c", 
     "types" : [ "restaurant", "lodging", "food", "casino", "establishment" ], 
     "vicinity" : "80 Pyrmont Street, Pyrmont" 
     }, 
     { 
     "geometry" : { 
      "location" : { 
       "lat" : -33.8656630, 
       "lng" : 151.191280 
      } 
     }, 
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png", 
     "id" : "78b3ab3969c71d9c4ca369a4c54e64291bc48013", 
     "name" : "Sugaroom", 
     "photos" : [ 
      { 
       "height" : 459, 
       "html_attributions" : [], 
       "photo_reference" : "CnRjAAAAQTgc4gBnL5qnXSxQZHPwSklMTAhlIFOQVcAnRP54JhttkSnfYayzoXqEFVf1FgEcZDePXR_ak5VrX94WtuXCsAxFHTUjruEFS6t-KZ1pZkV0lMJr5ScrkyQKbkz_4RC5ndMdkV-zk_BEGxehXtf1TxIQk6d-sL5CE4jTCkFpy-xxLBoUGDRSr7-DByGfguvq66dIRl2u4sE", 
       "width" : 612 
      } 
     ], 
     "price_level" : 3, 
     "rating" : 4.20, 
     "reference" : "CnRmAAAAJRV1ZXqRygLrNNpVgXdLT6UVb9rtttnEJBzui5bc8154vk7b0cUleXuKOypo1t9Wf9rJGp8qzq2zEGg2ExCKbZKRz_Nw-PlTKd5TuzSFjkgucSKYwmtd0LsgAK1rmgrYsm167g4_gSjFiDUlKvkbkxIQpgOyN66atH_smg785i8ZlRoUApskjjgRN2EZP6_u6mvYUd7HA2U", 
     "types" : [ "food", "bar", "establishment" ], 
     "vicinity" : "2/1 Harris Street, Pyrmont" 
     }, 
     { 
     "geometry" : { 
      "location" : { 
       "lat" : -33.8698080, 
       "lng" : 151.1971550 
      } 
     }, 
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png", 
     "id" : "f08757f3d7c5d879297b3cb715b33f1fae63536b", 
     "name" : "Oscars Lounge Bar and Restaurant", 
     "opening_hours" : { 
      "open_now" : false 
     }, 
     "photos" : [ 
      { 
       "height" : 640, 
       "html_attributions" : [], 
       "photo_reference" : "CpQBhgAAAFVwH5dzlEpvO1jsuTN6-I6bDc9Y3NUGbIOWpT7UjFJIVImCajv-kJYbObXXta1aR-eemuk4OgLA23rz5iNZcsqmWa9gt6tRoLmF5bO5ak4cgdQIUOJbf4LkxAKWYt5W1NGRMTqx_a6uf6SHQ0ftp-PuxneWKhpfddsMbvRE34pJnrgkJ8WftQf14V0jdQSSGRIQDODym-kvSkgFaTxEN-VRHxoUDohc30odzsSMhw3qcBEMrgfdP74", 
       "width" : 427 
      } 
     ], 
     "rating" : 3.50, 
     "reference" : "CoQBfgAAALY2CvGQEQSrPlpvCQX_NMpuuwD7iy9tgqS3RT7cnssgodZ9_cenagjN7QKTlZO8_31AdwVSdvANIdceZsPI3YGhUyPO-NfeuE9-fIMHDVH5fy08OsDpVZXAIgeilwKx-Gbf6heZhte3Ec7OVJKh2jTZSvGEd_Q8TcYeaBRxp4hTEhCrqMLPES46AhV0pKw3_9xNGhREzvfuZepHG9rLEdy2HLUVwdK6YQ", 
     "types" : [ "bar", "restaurant", "food", "establishment" ], 
     "vicinity" : "84 Union Street, Pyrmont" 
     } 
    ], 
    "status" : "OK" 
} 

這裏我IOS代碼:

- (IBAction)refreshTapped:(id)sender { 

    // Charge le fichie JSON 
    NSString *fileName = [[NSBundle mainBundle] pathForResource:@"map" ofType:@"json"]; 
    NSData *data = [NSData dataWithContentsOfFile:fileName]; 

    // Parse le fichier JSON 
    NSError *error; 
    NSDictionary *dico = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 
    NSArray *array = [dico valueForKeyPath:@"results"]; 

    if(error != nil) 
    { 
     NSLog(@"Erreur..."); 
    }else{ 
     NSLog(@"%@",array); 
    } 

    CLLocationCoordinate2D location; 
    NSMutableArray *newAnnotations = [NSMutableArray array]; 
    MKPointAnnotation *newAnnonation; 

    for (dico in array) { 

     // Récupère longitude et latitude 
     location.latitude = [dico[@"lat"] doubleValue]; 
     location.longitude = [dico[@"lng"] doubleValue]; 

     NSLog(@"Latitude: %f",[dico[@"lat"] doubleValue]); 
     NSLog(@"Longitude: %f",[dico[@"lng"] doubleValue]); 

     // Créer les annotations 
     newAnnonation = [[MKPointAnnotation alloc] init]; 
     newAnnonation.title = dico[@"name"]; 
     newAnnonation.coordinate = location; 

     // On ajoute l'objet au tableau 
     [newAnnotations addObject:newAnnonation]; 

    } 

    // On ajoute les annotations à la vue 
    [self._mapView addAnnotations:newAnnotations]; 

    // Ajouter la carte à la vue 
    [self.view insertSubview:__mapView atIndex:0]; 

} 

太謝謝你了!

回答

1

這是因爲latlng是陣列中的每個項目的子鍵:

"geometry" : { 
    "location" : { 
      "lat" : -33.8698080, 
      "lng" : 151.1971550 
    } 
    } 

因此,你必須retreive的完整路徑:

location.latitude = [[dico valueForKeyPath:@"geometry.location.lat"] doubleValue]; 
location.longitude = [[dico valueForKeyPath:@"geometry.location.lng"] doubleValue]; 
+0

完美!有用!非常感謝:D – Lapinou 2013-02-11 13:53:32

0
NSArray *array = [dico valueForKeyPath:@"results"]; 
for (NSDictionary *result in array) 
{ 
    NSDictionary *geometry = result[@"geometry"]; 
    NSDictionary *location = geometry[@"location"]; 
    location.latitude = [dico[@"lat"] doubleValue]; 
    location.longitude = [dico[@"lng"] doubleValue]; 
} 
+0

sory ..我不打算編輯你的答案。我正在編輯自己的答案:D。 – harshitgupta 2013-02-11 14:00:03

+0

沒關係..從我身邊拿+1;) – 2013-02-11 14:01:57

1
NSArray *array = [dictionary valueForKeyPath:@"results"]; 

NSDictionary *subDict; 
for (NSDictionary *dictionary in array) { 
    subDict = dictionary[@"geometry"]; 
    subDict = subDict[@"location"]; 
    DLog(@"%@", subDict[@"lat"]); 

}