【问题标题】:Accessing an array with nested objects and displaying keys values访问具有嵌套对象的数组并显示键值
【发布时间】:2013-12-30 05:22:53
【问题描述】:

我在这里有这个数据集: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 - 字典
  • 通过objectForKey访问pretty

我有这个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;
}

有没有人有任何例子或指导我的方向?希望有资源或问题可以解决。

【问题讨论】:

    标签: ios objective-c json


    【解决方案1】:
    -(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"]);
       }
    }
    

    【讨论】:

    • 谢谢,然后我会从我的initWithDict 方法中调用它?或者我会创建一个不同的类。信息将显示在同一视图上。我通过self.tideModel.XXX 显示我的其他人并更新我的标签文本。
    • 这取决于您的需要,通常不同的类是处理解析的 json 的更好方法。然后您可以轻松访问,否则必须使用 valueForKey。
    • 再次感谢您,我已经显示了。我想展示其中的几个部分。我如何将它绑定到模型以便我可以这样做:tide.data.typetide.date.pretty - 我查看了 JSONModel,但它需要数组,我有嵌套对象。想法?还是指向正确方向的指针?
    【解决方案2】:

    你可以看看Initialize NSArray with data from NSdictionary,因为我已经回答过了 希望这会有所帮助。

    【讨论】:

    • 哦,太好了,谢谢@Vish - 非常相似的问题和答案。
    • 好的。如果您能够解决您的问题,请标记此答案已解决。谢谢
    【解决方案3】:

    如果我理解你想要做什么,你应该可以替换这部分:

    // 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"]);
    }
    

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

    【讨论】:

      【解决方案4】:

      我在 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];
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-29
        • 2021-02-12
        • 2022-01-08
        • 1970-01-01
        • 2019-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多