gpt4 book ai didi

ios - 从NSJSONSerialization中获取数据中的值

转载 作者:行者123 更新时间:2023-12-01 17:26:15 24 4
gpt4 key购买 nike

我有一些从URL提取的JSON数据。我编写的代码可以很好地下载JSON并进行解析,但是我似乎也无法按需访问它,尤其是在数据包含在另一个元素中的情况下。

这是JSON格式:

{
address = "<null>";
city = "<null>";
country = UK;
"country_code" = GB;
daylight = 1;
for = daily;
items = (
{
asr = "5:22 pm";
"date_for" = "2013-7-1";
dhuhr = "1:01 pm";
fajr = "2:15 am";
isha = "11:47 pm";
maghrib = "9:24 pm";
shurooq = "4:39 am";
}
);
latitude = "50.9994081";
link = "http://muslimsalat.com/UK";
longitude = "0.5039011";
"map_image" = "http://maps.google.com/maps/api/staticmap?center=50.9994081,0.5039011&sensor=false&zoom=13&size=300x300";
"postal_code" = "<null>";
"prayer_method_name" = "Muslim World League";
"qibla_direction" = "119.26";
query = "51.000000,0.500000";
state = "<null>";
timezone = 0;
title = UK;
"today_weather" = {
pressure = 1020;
temperature = 14;
};
}

(这些是伊斯兰祈祷时间。)

到目前为止,我的Objective-C是:
-(CLLocationCoordinate2D) getLocation{
CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];

return coordinate;
}

//class to convert JSON to NSData
- (IBAction)getDataFromJson:(id)sender {
//get the coords:
CLLocationCoordinate2D coordinate = [self getLocation];
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];

NSLog(@"*dLatitude : %@", latitude);
NSLog(@"*dLongitude : %@",longitude);

//load in the times from the json
NSString *myURLString = [NSString stringWithFormat:@"http://muslimsalat.com/%@,%@/daily/5.json", latitude, longitude];
NSURL *url = [NSURL URLWithString:myURLString];
NSData *jsonData = [NSData dataWithContentsOfURL:url];

if(jsonData != nil)
{
NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
NSArray *jsonArray = (NSArray *)result; //convert to an array
if (error == nil)
NSLog(@"%@", result);
NSLog(@"%@", jsonArray);
for (id element in jsonArray) {
NSLog(@"Element: %@", [element description]);

}
}
}

运行此代码时,我得到的唯一输出是元素名称的列表( address, city, country,依此类推)。给出了 items,但未给出其子元素。我了解这是我要使用的代码:
for (id element in jsonArray) {
NSLog(@"Element: %@", [element description]);
}

但我不知道该如何进行下一步。

我唯一需要的数据值实际上是时间本身(例如 items>asritems>dhuhr等)。

我如何才能自己获取这些值,然后将它们另存为可以使用的值?

谢谢!

最佳答案

NSArray *jsonArray = (NSArray *)result; //convert to an array
这不是“转换”,只是您向编译器保证result实际上是NSArray。在这种情况下,这是一个谎言。

您的代码当前仅在JSON中返回的字典中打印键列表。尝试此操作以获取项目列表(它是一个数组,因此您需要处理可能存在多个条目):

NSDictionary *result = [NSJSONSerialization ...

for (NSDictionary *itemDict in result[@"items"]) {
NSLog(@"item: %@", itemDict);
}

然后,您可以提取时间。

关于ios - 从NSJSONSerialization中获取数据中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17398684/

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