帮助解析嵌套的 JSON 对象

发布于 2024-10-19 12:34:45 字数 4569 浏览 1 评论 0原文

我有以下 JSON

{
    "meta": {
        "code": 200
    }, 
    "response": {
        "deals": [
            {
                "id": 32373, 
                "date_added": "2011-01-13 12:12:50", 
                "end_date": "2011-01-14 10:00:00", 
                "active": 1, 
                "discount": {
                    "raw": 71, 
                    "formatted": "71%"
                }, 
                "price": {
                    "raw": "85.00", 
                    "formatted": "$85"
                }, 
                "value": {
                    "raw": "300.00", 
                    "formatted": "$300"
                }, 
                "purchased": 82, 
                "left": null, 
                "title": "$85 For $300 Babyface Facial At Park Avenue MedSpa", 
                "yipit_title": "71% off Babyface Facial", 
                "url": "http://yipit.com/aff/click/?deal=AvwTADtE&key=F374EFbM", 
                "yipit_url": "http://yipit.com/new-york/livingsocial/85-for-300-babyface-facial-at-park-avenue-medspa/", 
                "images": {
                    "image_big": "http://d22nv2k05ynu7x.cloudfront.net/deal_images/deal/85-for-300-babyface-facial-at-park-avenue-medspa-1294920769_display_image.jpg", 
                    "image_small": "http://d22nv2k05ynu7x.cloudfront.net/deal_images/deal/85-for-300-babyface-facial-at-park-avenue-medspa-1294920769_small_image.jpg"
                }, 
                "division": {
                    "slug": "new-york", 
                    "name": "New York", 
                    "active": 1, 
                    "time_zone_diff": -4, 
                    "lat": "40.7142690000000000", 
                    "lon": "-74.0059730000000000", 
                    "url": "http://yipit.com/new-york/"
                }, 
                "tags": [
                    {
                        "name": "Facial", 
                        "slug": "facial", 
                        "url": "http://yipit.com/new-york/deals/facial/"
                    }, 
                    {
                        "name": "Spa", 
                        "slug": "spa", 
                        "url": "http://yipit.com/new-york/deals/spa/"
                    }
                ], 
                "business": {
                    "name": "Park Avenue MedSpa", 
                    "url": "", 
                    "locations": [
                        {
                            "address": "565 Park Ave", 
                           "locality": "New York",
                            "phone": "212-593-8821", 
                            "lat": null, 
                            "lon": null
                        }
                    ]
                }, 
                "source": {
                    "name": "LivingSocial", 
                    "slug": "livingsocial", 
                    "paid": 0, 
                    "url": "http://yipit.com/new-york/livingsocial"
                }
            }
        ]
    }
}

这是我解析它的方式:

    NSArray *deals = [[results objectForKey:@"response"] valueForKey:@"deals"];
    NSLog(@"%@",[[results objectForKey:@"response"] valueForKey:@"deals"]);

    for(NSDictionary* deal in deals)
    {
        NSLog(@"Title: %@", [deal valueForKey:@"title"]);
        NSLog(@"URL: %@", [deal valueForKey:@"url"]);
        NSLog(@"Value: %@", [[deal valueForKey:@"value"] valueForKey:@"formatted"]);
        NSLog(@"Picture URL: %@", [[deal valueForKey:@"images"] valueForKey:@"image_small"]);
        NSLog(@"Business: %@", [[deal valueForKey:@"business"] valueForKey:@"name"]);
        NSLog(@"Address: %@", [[[deal valueForKey:@"business"] valueForKey:@"locations"] valueForKey:@"address"]);
        NSLog(@"City: %@", [[[deal valueForKey:@"business"] valueForKey:@"locations"] valueForKey:@"locality"]);
        NSLog(@"Phone: %@", [[[deal valueForKey:@"business"] valueForKey:@"locations"] valueForKey:@"phone"]);

        [items addObject:deal];
    }

除了地址城市之外,我的日志记录看起来不错> 和电话。这是日志:

Title: $10 for $20 Worth of Food and Drinks at Blondies in Tempe
URL: http://yipit.com/aff/click/?deal=cHBPNZ3w&key=93HU7t2d
Value: $20
Picture URL: http://d22nv2k05ynu7x.cloudfront.net/deal_images/deal/50-off-at-blondies-sports-bar-1298454745_small_image.jpg
Business: Blondies Sports Bar
Address: (
    "501 S Mill Ave"
)
City: (
    Tempe
)
Phone: (
    "702-737-0444"
)

I have the following JSON:

{
    "meta": {
        "code": 200
    }, 
    "response": {
        "deals": [
            {
                "id": 32373, 
                "date_added": "2011-01-13 12:12:50", 
                "end_date": "2011-01-14 10:00:00", 
                "active": 1, 
                "discount": {
                    "raw": 71, 
                    "formatted": "71%"
                }, 
                "price": {
                    "raw": "85.00", 
                    "formatted": "$85"
                }, 
                "value": {
                    "raw": "300.00", 
                    "formatted": "$300"
                }, 
                "purchased": 82, 
                "left": null, 
                "title": "$85 For $300 Babyface Facial At Park Avenue MedSpa", 
                "yipit_title": "71% off Babyface Facial", 
                "url": "http://yipit.com/aff/click/?deal=AvwTADtE&key=F374EFbM", 
                "yipit_url": "http://yipit.com/new-york/livingsocial/85-for-300-babyface-facial-at-park-avenue-medspa/", 
                "images": {
                    "image_big": "http://d22nv2k05ynu7x.cloudfront.net/deal_images/deal/85-for-300-babyface-facial-at-park-avenue-medspa-1294920769_display_image.jpg", 
                    "image_small": "http://d22nv2k05ynu7x.cloudfront.net/deal_images/deal/85-for-300-babyface-facial-at-park-avenue-medspa-1294920769_small_image.jpg"
                }, 
                "division": {
                    "slug": "new-york", 
                    "name": "New York", 
                    "active": 1, 
                    "time_zone_diff": -4, 
                    "lat": "40.7142690000000000", 
                    "lon": "-74.0059730000000000", 
                    "url": "http://yipit.com/new-york/"
                }, 
                "tags": [
                    {
                        "name": "Facial", 
                        "slug": "facial", 
                        "url": "http://yipit.com/new-york/deals/facial/"
                    }, 
                    {
                        "name": "Spa", 
                        "slug": "spa", 
                        "url": "http://yipit.com/new-york/deals/spa/"
                    }
                ], 
                "business": {
                    "name": "Park Avenue MedSpa", 
                    "url": "", 
                    "locations": [
                        {
                            "address": "565 Park Ave", 
                           "locality": "New York",
                            "phone": "212-593-8821", 
                            "lat": null, 
                            "lon": null
                        }
                    ]
                }, 
                "source": {
                    "name": "LivingSocial", 
                    "slug": "livingsocial", 
                    "paid": 0, 
                    "url": "http://yipit.com/new-york/livingsocial"
                }
            }
        ]
    }
}

Here is how I am parsing it:

    NSArray *deals = [[results objectForKey:@"response"] valueForKey:@"deals"];
    NSLog(@"%@",[[results objectForKey:@"response"] valueForKey:@"deals"]);

    for(NSDictionary* deal in deals)
    {
        NSLog(@"Title: %@", [deal valueForKey:@"title"]);
        NSLog(@"URL: %@", [deal valueForKey:@"url"]);
        NSLog(@"Value: %@", [[deal valueForKey:@"value"] valueForKey:@"formatted"]);
        NSLog(@"Picture URL: %@", [[deal valueForKey:@"images"] valueForKey:@"image_small"]);
        NSLog(@"Business: %@", [[deal valueForKey:@"business"] valueForKey:@"name"]);
        NSLog(@"Address: %@", [[[deal valueForKey:@"business"] valueForKey:@"locations"] valueForKey:@"address"]);
        NSLog(@"City: %@", [[[deal valueForKey:@"business"] valueForKey:@"locations"] valueForKey:@"locality"]);
        NSLog(@"Phone: %@", [[[deal valueForKey:@"business"] valueForKey:@"locations"] valueForKey:@"phone"]);

        [items addObject:deal];
    }

My logging looks good except for Address, City and Phone. Here is the log:

Title: $10 for $20 Worth of Food and Drinks at Blondies in Tempe
URL: http://yipit.com/aff/click/?deal=cHBPNZ3w&key=93HU7t2d
Value: $20
Picture URL: http://d22nv2k05ynu7x.cloudfront.net/deal_images/deal/50-off-at-blondies-sports-bar-1298454745_small_image.jpg
Business: Blondies Sports Bar
Address: (
    "501 S Mill Ave"
)
City: (
    Tempe
)
Phone: (
    "702-737-0444"
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假装不在乎 2024-10-26 12:34:45

从您提供的 JSON 中,locations 是一个数组(这就是方括号的含义),因此从 valueForKey: 返回的值包含在 NSArray 实例中(因此括号)。

您可以在最后一次调用 valueForKey: 之前使用 objectAtIndex: 来获取您要查找的值。

顺便说一句,您还可以使用 valueForKeyPath: 来简化访问嵌套值,例如

[deal valueForKeyPath:@"business.name"];

From the JSON you provided, locations is an array (that's what the square brackets mean), so the values that are returned from valueForKey: are contained in NSArray instances (hence the parentheses).

You could use objectAtIndex: before the last call to valueForKey: to obtain just the value you're looking for.

By the way, you can also use valueForKeyPath: to simplify accessing nested values, for example

[deal valueForKeyPath:@"business.name"];
慢慢从新开始 2024-10-26 12:34:45

[[deal valueForKey:@"business"] valueForKey:@"locations"] 返回一个数组。在 NSArray 上调用 -valueForKey: 会对数组中的每个对象执行 valueForKey: 并返回结果数组(请参阅 NSArray 文档这)。这就是您得到的,相应值的数组。

[[deal valueForKey:@"business"] valueForKey:@"locations"] returns an array. Calling -valueForKey: on an NSArray executes valueForKey: for every object in the array and returns an array of the results (see the NSArray documentation for this). So that's what you get, an array of the corresponding values.

独自←快乐 2024-10-26 12:34:45

您可以尝试将代码更改为如下所示:

NSDictionary *primaryLocation =  [[[deal valueForKey:@"business"] valueForKey:@"locations"] objectAtIndex:0];
NSLog(@"Address: %@", [primaryLocation valueForKey:@"address"]);
NSLog(@"City: %@", [primaryLocation valueForKey:@"locality"]);
NSLog(@"Phone: %@", [primaryLocation valueForKey:@"phone"]);

You could try changing your code to something like this:

NSDictionary *primaryLocation =  [[[deal valueForKey:@"business"] valueForKey:@"locations"] objectAtIndex:0];
NSLog(@"Address: %@", [primaryLocation valueForKey:@"address"]);
NSLog(@"City: %@", [primaryLocation valueForKey:@"locality"]);
NSLog(@"Phone: %@", [primaryLocation valueForKey:@"phone"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文