使用嵌套对象访问数组并显示键值

时间:2013-12-30 05:22:53

标签: ios objective-c json

我在这里有这个数据集:https://gist.github.com/ryancoughlin/8043604 - 如果你看到tide.tideSummary它包含一个数组但在里面,它包含多个字典。我正在尝试访问并显示tide.tideSummary.date.pretty并在某种类型的表中显示(所有33个)(但我使用了虚拟数据)。

我在这里遇到了一个类似的问题:Keypath for first element in embedded NSArray

在找到访问这些嵌套值的解决方案时遇到一些麻烦。通过StackOverflow上的大量教程和帖子处理非常基本的JSON字符串,这些对我来说很有用。


更新:

遇到了这个问题:Parsing nested JSON objects with JSON Framework for Objective-C

就我而言,我有以下内容:

NSArray *prettyDate = [[tide objectForKey:@"tideSummary"] valueForKey:@"date"];

prettyDateNSLog

打印出来
Pretty date array: (
        {
        epoch = 1388388109;
        hour = 02;
        mday = 30;
        min = 21;
        mon = 12;
        pretty = "2:21 AM EST on December 30, 2013";
        tzname = "America/New_York";
        year = 2013;
    },
        {
        epoch = 1388397506;
        hour = 04;
        mday = 30;
        min = 58;
        mon = 12;
        pretty = "4:58 AM EST on December 30, 2013";
        tzname = "America/New_York";
        year = 2013;
    },
        {
        epoch = 1388405656;
        hour = 07;
        mday = 30;
        min = 14;
        mon = 12;
        pretty = "7:14 AM EST on December 30, 2013";
        tzname = "America/New_York";
        year = 2013;
    }

然后我可以遍历每个,抓住对象并显示?


我会做类似的事情吗?

  • 抓取父项tideSummary - 数组
  • 抓住上面的项目并存储date - 词典
  • 通过pretty
  • 访问objectForKey

我有initWithDict

-(id)initWithDict:(NSDictionary *)json {
    self = [super init];

    if(self) {

        NSDictionary *tide = [json valueForKeyPath:@"tide"];

        NSArray *arrOfTideSummaryStats = [json valueForKeyPath:@"tide.tideSummaryStats"];

        NSDictionary *dctOfTideSummaryStats = [arrOfTideSummaryStats objectAtIndex:0];

        NSArray *arrOfTideSummary = [json valueForKeyPath:@"tide.tideSummary"];

        // Loop through the date then...
        // Loop and grab 'pretty'

        // The "first" is the from the example link in my question above. Experimental oonly
        id pretty = [arrOfTideSummary valueForKeyPath: @"@first.tideSummary.date.pretty"];

        // This all works below

        self.maxheight = [NSString stringWithFormat:@"%@", [dctOfTideSummaryStats valueForKey: @"maxheight"]];
        self.minheight = [NSString stringWithFormat:@"%@", [dctOfTideSummaryStats valueForKey: @"minheight"]];

        /*============================================================*/

        NSArray *arrOfTideInfo = [json valueForKeyPath:@"tide.tideInfo"];
        NSDictionary *dctOfTideInfo = [arrOfTideInfo objectAtIndex:0];

        self.tideSite = [dctOfTideInfo valueForKey:@"tideSite"];
    }

    return self;
}

有没有人有任何例子或方向引导我进去?会喜欢一种资源或问题来解决问题。

4 个答案:

答案 0 :(得分:4)

-(void)parseJson:(id)json{
   NSDictionary *tide = [json objectForKey:@"tide"];
   NSArray *tideSummary = [tide objectForKey:@"tideSummary"];

   for (int i = 0; i < tideSummary.count; i++) {  
      NSDictionary *eachTideSummary = [tideSummary objectAtIndex:i];

      NSDictionary *dateDic = [eachTideSummary objectForKey:@"date"];
      NSDictionary *utcdateDic = [eachTideSummary objectForKey:@"utcdate"];

      NSLog(@"Preety from date dictionary: %@", [dateDic objectForKey:@"pretty"]);
      NSLog(@"Preety from utcdate dictionary: %@", [utcdateDic objectForKey:@"pretty"]);
   }
}

答案 1 :(得分:1)

您可以查看Initialize NSArray with data from NSdictionary,因为我已经回答了这个问题  希望这会有所帮助。

答案 2 :(得分:0)

如果我理解你要做什么,你应该能够替换这部分:

// Loop through the date then...
// Loop and grab 'pretty'

有类似的东西:

for (NSDictionary *tideSummary in arrOfTideSummary) {
    NSDictionary *date = [tideSummary valueForKey:@"date"];
    NSLog(@"Pretty date: %@", [date valueForKey:@"pretty"]);
}

如果您想要某种表中的日期值,那么您可以将它们存储到新数组或其他数据结构中,然后创建一个表视图,将其用作数据源。

答案 3 :(得分:0)

我在JSON链接上有以下数据......

{
"results":[
    {
        "address_components": [
            {
                "long_name": "Satadhar",
                "short_name": "Satadhar",
                "types":[
                    "point_of_interest",
                    "establishment"
                ]
            } ]
]

我通过ASIHTTPREQUEST获取数据,因此代码低于给定

-(void)requestFinished:(ASIHTTPRequest *)request {
        NSError *err;
        NSData *data=[request responseData];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data   options:kNilOptions error:&err];

        arr=[[dict valueForKey:@"results"]valueForKey:@"address_components"]; //to fetch the data till inside Starting point i.e address_component

        _arrTable=[[NSMutableArray alloc]init]; //take a array to store the data of the index 0 [index 0 store the whole data that is start from [array] ]

        _arrTable =[arr objectAtIndex:0]; //get the data of 0th index

        NSLog(@" whole data%@",_arrTable);//print it u will get the idea

        NSLog(@" my %@",[[arr objectAtIndex:0] valueForKey:@"long_name"]);
        NSLog(@" my %@",[[arr objectAtIndex:0] valueForKey:@"short_name"]);

        [self.tblview reloadData];
    }