如何从JSON字典中有效地提取数据

时间:2015-03-30 16:21:40

标签: ios objective-c json

我最近使用以下代码从Foursquare API调用中提取位置ID:

NSDictionary* foursquareJson = [NSJSONSerialization JSONObjectWithData:secureData options:kNilOptions error:&error];
NSDictionary *venuesDict = foursquareJson[@"response"];
NSArray *venuesArray = venuesDict[@"venues"];
NSDictionary *venuesDict2 = venuesArray[0];
NSArray *categoriesDict = venuesDict2[@"categories"];
NSDictionary *idDict = categoriesDict[0];
NSLog(@"ID is %@",idDict[@"id"]);

原版foursquareJson是:

2015-03-30 17:16:40.700 Voyagic[2833:718563] {
    meta =     {
        code = 200;
    };
    response =     {
        venues =         (
                        {
                categories =                 (
                                        {
                        icon =                         {
                            prefix = "https://ss3.4sqi.net/img/categories_v2/building/conventioncenter_";
                            suffix = ".png";
                        };
                        id = 4bf58dd8d48988d1ff931735;
                        name = "Convention Center";
                        pluralName = "Convention Centers";
                        primary = 1;
                        shortName = "Convention Center";
                    }
                );
                contact =                 {
                    formattedPhone = "+44 20 7222 5000";
                    phone = "+442072225000";
                };
                hereNow =                 {
                    count = 0;
                    groups =                     (
                    );
                    summary = "Nobody here";
                };
                id = 4b6599d4f964a520f8f52ae3;
                location =                 {
                    address = "Broad Sanctuary";
                    cc = GB;
                    city = London;
                    country = "United Kingdom";
                    distance = 2167;
                    formattedAddress =                     (
                        "Broad Sanctuary",
                        London,
                        "Greater London",
                        "SW1P 3EE",
                        "United Kingdom"
                    );
                    lat = "51.49997800145596";
                    lng = "-0.1289014132864838";
                    postalCode = "SW1P 3EE";
                    state = "Greater London";
                };
                name = "Queen Elizabeth II Conference Centre";
                referralId = "v-1427732200";
                specials =                 {
                    count = 0;
                    items =                     (
                    );
                };
                stats =                 {
                    checkinsCount = 3657;
                    tipCount = 15;
                    usersCount = 2407;
                };
                verified = 0;
            }
        );
    };
}

但肯定必须有更好的方式来访问我不知道的ID(而不是创建4个词典和2个数组,这似乎有些过分:/)。任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:1)

最终,需要通过字典和数组列表以某种方式访问​​数据,它只取决于您希望发生的位置。您可以为JSON使用或创建解析器,但最终仍需要将JSON数据映射到您正在执行的操作。访问数据的一种简单而简单的方法是不在每次迭代中创建新变量。虽然它确实没有太大的不同:

NSDictionary *foursquareJson = [NSJSONSerialization JSONObjectWithData:secureData options:kNilOptions error:&error];
NSDictionary *objectId = foursquareJson[@"response"][@"venues"][0][@"categories"][0][@"id"];
NSLog(@"ID is %@",objectId);

由于这个答案的评论中的对话,我认为我应该包含更多关于你创建“4个字典和2个数组”的关注的信息。当您使用JSON Serializer从JSON(上面的第一行)创建本机对象时,您将创建完全表示和存储整个JSON所需的所有数组和字典。您最初发布的内容与我提供的内容之间的代码示例差异实际上并没有太大差异。如果您担心创建太多字典或数组,则应在将其反序列化为本机对象之前尝试过滤掉JSON。