gpt4 book ai didi

ios - 如何在 Objective C 中从 NSArray 中获取元素

转载 作者:行者123 更新时间:2023-11-29 01:05:52 26 4
gpt4 key购买 nike

我正在使用公交时刻表 API,并希望根据用户当前位置显示附近的公交车站。我已经通过 url 获得了 JSON 响应并将 JSON 解析为 NSArray。 NSArray 看起来像这样。

{
result = {
distance = "0.00003292029";
lat = "-37.92091";
"location_name" = "Monash Medical Centre/Clayton Rd ";
lon = "145.120682";
"route_type" = 2;
"stop_id" = 16518;
suburb = Clayton;
"transport_type" = bus;
};
type = stop;
},
{
result = {
distance = "0.00003728643";
lat = "-37.92227";
"location_name" = "Monash Surgical Private Hospital/291 Clayton Rd ";
lon = "145.1202";
"route_type" = 2;
"stop_id" = 24348;
suburb = Clayton;
"transport_type" = bus;
};
type = stop;
},
{
result = {
distance = "0.00003804303";
lat = "-37.9230766";
"location_name" = "Clayton Railway Station/Clayton Rd ";
lon = "145.120209";
"route_type" = 2;
"stop_id" = 22809;
suburb = Clayton;
"transport_type" = bus;
};
type = stop;
},
{
result = {
distance = "0.00003976311";
lat = "-37.9186172";
"location_name" = "Monash Specialist Centre/Clayton Rd ";
lon = "145.121033";
"route_type" = 2;
"stop_id" = 22807;
suburb = Clayton;
"transport_type" = bus;
};
type = stop;
},
{
result = {
distance = "0.00004085229";
lat = "-37.9186478";
"location_name" = "Dixon St/Clayton Rd ";
lon = "145.120911";
"route_type" = 2;
"stop_id" = 13889;
suburb = Clayton;
"transport_type" = bus;
};
type = stop;
}

)

我的问题是如何从这个 NSArray 中获取纬度和经度。我写了这些代码,但它返回 nil。

    NSMutableArray *coordinateLatit = [[NSMutableArray alloc]init];
for (int i = 0; i<nearbyStop.count; i++) {
NSString *latit = [[nearbyStop objectAtIndex:1] objectForKey:@"lat"];
if(latit == nil)
{
NSLog(@"there is no data");
}
else
{
[coordinateLatit addObject:latit];
}
}

我还想在 MKMapView 上显示停靠点(使用 NSArray 的经度和纬度),我应该如何实现 MKLocalSearchRequest如果有人能帮助我,我将不胜感激!

最佳答案

您的代码忽略了额外的嵌套 result 级别。

NSMutableArray *coordinateLatit=[[NSMutableArray alloc]init];

for(NSDictionary *d in nearbyStop)
{
NSString *latit=d[@"result"][@"lat"];

if(lat) [coordinateLatit addObject:latit];
}

关于ios - 如何在 Objective C 中从 NSArray 中获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36374094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com