0

我在获取 JSON 文件中的特定密钥时遇到了一些问题。事实上,我使用的是 MapView 和带有一些餐馆坐标的 JSON 文件。

我可以显示我的 JSON 文件的内容,但我无法获取关键内容:lat(纬度)和 lng(经度)。

这是我的 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];

}

非常感谢!

4

3 回答 3

1

那是因为latandlng是数组中每个项目的子键:

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

因此,您必须检索完整路径:

location.latitude = [[dico valueForKeyPath:@"geometry.location.lat"] doubleValue];
location.longitude  = [[dico valueForKeyPath:@"geometry.location.lng"] doubleValue]; 
于 2013-02-11T13:48:20.440 回答
1
NSArray *array = [dictionary valueForKeyPath:@"results"];

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

}
于 2013-02-11T13:57:30.790 回答
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];
}
于 2013-02-11T13:56:02.960 回答