Ios 从NSJSONSerialization获取数据值

Ios 从NSJSONSerialization获取数据值,ios,objective-c,json,nsjsonserialization,Ios,Objective C,Json,Nsjsonserialization,我有一些从URL中提取的JSON数据。我编写的代码可以很好地下载JSON并对其进行解析,但我似乎也无法根据需要访问它,尤其是在数据作为另一个JSON的子元素包含的情况下 以下是JSON格式: { address = "<null>"; city = "<null>"; country = UK; "country_code" = GB; daylight = 1; for = daily; items = (

我有一些从URL中提取的JSON数据。我编写的代码可以很好地下载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;
    };
}
运行此代码时,我得到的唯一输出是元素名称列表(
地址、城市、国家等)<代码>项
是给定的,但不是其子元素。我明白这就是我要求代码的目的:

for (id element in jsonArray) {
    NSLog(@"Element: %@", [element description]);
}
但我不知道如何进入下一步

我需要的唯一数据值实际上是时间本身(因此,
items>asr
items>dhuhr
,等等)

如何获取这些值,然后将它们保存为可以使用的值

谢谢大家!

(…);-is数组

{…};-是字典吗

所以你的“元素”是字典

使用objectForKey:

例如:

for (id element in jsonArray) {
    NSLog(@"Element asr: %@", [element objectForKey:@"asr"]); // or element[@"asr"]
}
(……);-is数组

{…};-是字典吗

所以你的“元素”是字典

使用objectForKey:

例如:

for (id element in jsonArray) {
    NSLog(@"Element asr: %@", [element objectForKey:@"asr"]); // or element[@"asr"]
}
NSArray*jsonArray=(NSArray*)结果//转换为数组

这不是“转换”,只是你向编译器保证,
result
实际上是一个
NSArray
。在这种情况下,这是一个谎言

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

然后你可以提取时间

NSArray*jsonArray=(NSArray*)结果//转换为数组

这不是“转换”,只是你向编译器保证,
result
实际上是一个
NSArray
。在这种情况下,这是一个谎言

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


然后您可以提取时间。

您可以通过以下方式提取信息:

 NSError* error = nil;
 NSDictionary *userInfo; //your main data

 if([NSJSONSerialization class])
  userInfo = [NSJSONSerialization JSONObjectWithData:[request responseData] options:kNilOptions error:&error];

//to extract items
  NSDictionary *items = [[[userInfo objectForKey:@"items"] JSONValue] objectAtIndex:0];

您可以通过以下方式提取信息:

 NSError* error = nil;
 NSDictionary *userInfo; //your main data

 if([NSJSONSerialization class])
  userInfo = [NSJSONSerialization JSONObjectWithData:[request responseData] options:kNilOptions error:&error];

//to extract items
  NSDictionary *items = [[[userInfo objectForKey:@"items"] JSONValue] objectAtIndex:0];

嗯,这看起来不错,但当我运行这个应用程序时,它退出了。它进入
线程1:SIGBART
并输出堆栈跟踪。知道为什么吗?嗯,这对我来说很好,但当我运行这个应用程序时,它就退出了。它进入
线程1:SIGBART
并输出堆栈跟踪。知道为什么吗?