【问题标题】:Count how many of a certain object appear in a JSON query计算某个对象在 JSON 查询中出现的数量
【发布时间】:2012-11-06 09:18:52
【问题描述】:

我返回的 JSON 具有如下所示的粗略结构,我试图弄清楚如何计算有多少平台(在本例中为三个,但可以是 1 到 20 或所以)。我已将 JSON 返回到 NSDictionary 并使用诸如此类的行来检索我需要的数据:

_firstLabel.text = _gameDetailDictionary[@"results"][@"name"];

在上述情况下,它将从results 部分获取name。由于有多个平台,我需要构造一个循环来循环遍历platforms 部分内的每个name。不太清楚该怎么做。感谢所有帮助!

"results":{
    "platforms":[
        {
            "api_detail_url":"http://",
            "site_detail_url":"http://",
            "id":18,
            "name":"First Name"
        },
        {
            "api_detail_url":"http://",
            "site_detail_url":"http://",
            "id":116,
            "name":"Second Name"
        },
        {
            "api_detail_url":"http://",
            "site_detail_url":"http://",
            "id":22,
            "name":"Third Name"
        }
    ],

编辑:这是我的 fetchJSON 方法:

- (NSDictionary *) fetchJSONDetail: (NSString *) detailGBID {

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: YES];

    NSString *preparedDetailURLString = [NSString stringWithFormat:@"http://whatever/format=json", detailGBID];
    NSLog(@"Doing a detailed search for game ID %@", detailGBID);

    NSData *jsonData = [NSData dataWithContentsOfURL: [NSURL URLWithString:preparedDetailURLString]];

    _resultsOfSearch = [[NSDictionary alloc] init];
    if (jsonData) {
        _resultsOfSearch = [NSJSONSerialization JSONObjectWithData: jsonData
                                                           options: NSJSONReadingMutableContainers
                                                             error: nil];
    }

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: NO];

    NSString *results = _resultsOfSearch[@"number_of_page_results"];
    _numberOfSearchResults = [results intValue];

    NSArray *platforms = [_resultsOfSearch valueForKey:@"platforms"];
    int platformsCount = [platforms count];
    NSLog(@"This game has %d platforms!", platformsCount);

    return _resultsOfSearch;

}

【问题讨论】:

    标签: ios json for-loop nsdictionary nsjsonserialization


    【解决方案1】:

    “平台”JSON 字段是一个数组,因此假设您已使用类似的方式对 JSON 进行反序列化,

    NSMutableDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:resultsData options:NSJSONReadingMutableContainers error:&error];
    

    然后,您可以将平台分配给 NSArray,

    NSDictionary *results = [responseJSON valueForKey:@"results"];
    
    NSArray *platforms = [results valueForKey:@"platforms"];
    

    ...并通过,找到平台的数量,

    int platformsCount = [platforms count];
    

    在您的情况下,您可以在想要迭代平台的地方使用,

    for (NSDictionary *platform in platforms)
    {
        // do something for each platform
    }
    

    【讨论】:

    • 看起来不错,但似乎不起作用。我在上面添加了我的方法,我已经添加了你的 NSArray 和 int ,但是下面的 NSLog 每次都返回零。 platforms 在 JSON 的嵌套中的位置是否重要,或者它会在任何可能的地方寻找它?
    • 我的错误,我没有从最初的字典中提取@"results" 字段以将其降低一个级别。我已经编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多