【问题标题】:JSON with Dictionary - nested objects to convert to strings and displayJSON with Dictionary - 嵌套对象转换为字符串并显示
【发布时间】:2013-12-28 17:56:02
【问题描述】:

我在这里看到了一些与我正在做的事情相关的帖子,但我正在处理一些我想要提取的嵌套对象。

这是我返回的数据示例 - https://gist.github.com/ryancoughlin/8043604

到目前为止,我的标题中有这个:

#import "TideModel.h"

@protocol TideModel
@end

@implementation TideModel

-(id)initWithDict:(NSDictionary *)json {
    self = [super init];
    if(self) {
        self.maxheight = [dictionary valueForKeyPath:@"tide.tideSummaryStats.minheight"];
        self.minheight = [dictionary valueForKeyPath:@"tide.tideSummaryStats.maxheight"];
        self.tideSite = [dictionary valueForKeyPath:@"tide.tideInfo.tideSite"];
    }
    return self;
}
@end

我已经为每个字符串声明了一个property,我正在相应地访问它。
但是我上面的内容不起作用,也许是因为它不知道要纠正什么?...或者会吗?

【问题讨论】:

  • tide.tideSummaryStats 返回一个数组。事实上,即使tide.tideInfo 返回一个数组。另外,不应该是[dictionary valueForKeyPath:...],应该是[json valueForKeyPath:...]

标签: ios objective-c json cocoa-touch key-value-coding


【解决方案1】:
  1. tide.tideSummaryStats 返回一个数组。
  2. tide.tideInfo 返回一个数组。

所以你不能一直做-valueForKeyPath:


另外,这是不正确的:[dictionary valueForKeyPath:...];
应该是:[json valueForKeyPath:...];

因为json 是传递的NSDictionary 变量的名称(不是dictionary


试试这个(不确定):

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

    if(self) {
        NSArray *arrOfTideSummaryStats = [json valueForKeyPath:@"tide.tideSummaryStats"];
        NSDictionary *dctOfTideSummaryStats = [arrOfTideSummaryStats objectAtIndex:0];

        //since self.maxheight and self.minheight are NSString objects and 
        //the original keys "minheight" & "maxheight" return float values, do:
        self.maxheight = [NSString stringWithFormat:@"%f", [dctOfTideSummaryStats valueForKey: @"maxheight"]];
        self.minheight = [NSString stringWithFormat:@"%f", [dctOfTideSummaryStats valueForKey: @"minheight"]];

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

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

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

    return self;
}

类似问题:

【讨论】:

  • 随着越来越多的项目增长,我会在这里整理它们,还是继续分解它们是最佳做法? (还是更多地取决于案例和数据类型?)如果您看到 summaryStats 是一个包含不想显示天数的数组。
  • @Coughlin :视情况而定,但基本上,如果您只有嵌套字典,那么 KVC 会有很大帮助,但是当中间有一个数组(具有多个索引)时,那么KVC就不能很好地工作了。 check: link(已经很老了,2011 年,我不确定现在情况是否有所不同)
  • @Coughlin :在答案中添加了一个可能对您有所帮助的链接。 check: link
  • 好的,太好了。现在正在阅读。这样可以很好地使用 JSONModel 之类的框架吗?结合上面的代码来显示键值?
  • 我试过它确实显示我所有的数据,但我认为它是矫枉过正,因为它主要寻找数组。
【解决方案2】:

最近不得不创建一个与远程 RESTful 服务器一起工作的应用程序,该服务器返回 JSON 数据,然后反序列化为图形对象。

我对请求和响应使用了 unirest,然后将返回的 JSON 反序列化为一个对象。下面是代码的摘录,其中字典“jsonResponseAsDictionary”中的“hourlySalesFigures”是我放入数组中的 24 个数字的 JSON 集合。请注意,该功能要大得多,但我删除了我认为会分散注意力的所有内容。

- (PBSSales*) deserializeJsonPacket2:(NSDictionary*)jsonResponseAsDictionary withCalenderType:(NSString *)calendarViewType
{
    PBSSales *pbsData = [[PBSSales alloc] init];

    if(jsonResponseAsDictionary != nil)
    {
        // Process the hourly sales figures if the day request and returned is related to Daily figures
        if([calendarViewType isEqualToString:@"Day"]){
            NSArray *hourlyFiguresFromJson = [jsonResponseAsDictionary objectForKey:@"hourlySalesFigures"];

            PBSDataDaySales *tmpDataDay = [[PBSDataDaySales alloc] init];
            NSMutableArray *hSalesFigures = [tmpDataDay hourlySalesFigures];

            for(NSInteger i = 0; i < [hourlyFiguresFromJson count]; i++){
                hSalesFigures[i] = hourlyFiguresFromJson[i];
            }

            [[pbsData dataDay] setHourlySalesFigures:hSalesFigures];
            [pbsData setCalViewType:@"Day"];
        }

    }

    return pbsData;
}

【讨论】:

    猜你喜欢
    • 2017-09-13
    • 2023-01-16
    • 2021-02-05
    • 2019-12-22
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    相关资源
    最近更新 更多