将NSDictionaries和Arrays的复杂嵌套解析为更简单的NSDictionary

时间:2012-12-17 01:22:00

标签: objective-c json nsdictionary

我在这里有点过头了(实际上很多)。我有一个JSON响应,已被转换为NSDictionary。这是阵列中字典中的嵌套数组和字典的混乱......我不知道如何处理它。我想要做的是为每个“item”创建一个简单的NSDictionary,并使用属性来驱动UITableView。我不知道如何访问几个级别的对象。我已经做了一些搜索,我发现有几种方法可以解决它(快速枚举,块等等)但在开始旋转我的轮子之前,我很想知道什么对我的特殊情况最好。为了便于阅读,我将NSDictionary保存为plist。我会在这里发布:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>search_result</key>
    <dict>
        <key>latitude</key>
        <string>51.508129</string>
        <key>longitude</key>
        <string>-0.128005</string>
        <key>page</key>
        <string>1</string>
        <key>per_page</key>
        <integer>3</integer>
        <key>results</key>
        <array>
            <dict>
                <key>result</key>
                <dict>
                    <key>description_short</key>
                    <string>Experience the history and mystery of the most famous sites in the South of England!</string>
                    <key>flex_reference</key>
                    <string>FLX-LON-77B-D2F-5ED5</string>
                    <key>geocoded_latitude</key>
                    <string>51.5081289</string>
                    <key>geocoded_longitude</key>
                    <string>-0.128005</string>
                    <key>name</key>
                    <string>Stonehenge, Glastonbury, Avebury and Chalice Well</string>
                    <key>primary_image</key>
                    <string>http://media.****.com/FLX-LON-77B-D2F-5ED5-image_original-56.jpg</string>
                    <key>product_categories</key>
                    <array>
                        <dict>
                            <key>product_category</key>
                            <dict>
                                <key>kind</key>
                                <string>type</string>
                                <key>name</key>
                                <string>Sightseeing Tours</string>
                            </dict>
                        </dict>
                        <dict>
                            <key>product_category</key>
                            <dict>
                                <key>kind</key>
                                <string>category</string>
                                <key>name</key>
                                <string>Excursion</string>
                            </dict>
                        </dict>
                        <dict>
                            <key>product_category</key>
                            <dict>
                                <key>kind</key>
                                <string>type</string>
                                <key>name</key>
                                <string>Attractions</string>
                            </dict>
                        </dict>
                    </array>
                </dict>
            </dict>
            <dict>
                <key>result</key>
                <dict>
                    <key>description_short</key>
                    <string>Experience some of the most famous landmarks in English history, University City of Oxford, rolling countryside and honey stoned cottages of the Cotswolds and Stratford upon Avon home of the famous English plyright, William Shakespeare.</string>
                    <key>flex_reference</key>
                    <string>FLX-LON-2AD-267-38AB</string>
                    <key>geocoded_latitude</key>
                    <string>51.5081289</string>
                    <key>geocoded_longitude</key>
                    <string>-0.128005</string>
                    <key>name</key>
                    <string>Oxford, Stratford and the Cotswolds Villages</string>
                    <key>primary_image</key>
                    <string>http://media.*****.com/FLX-LON-2AD-267-38AB-image_original-AF.jpg</string>
                    <key>product_categories</key>
                    <array>
                        <dict>
                            <key>product_category</key>
                            <dict>
                                <key>kind</key>
                                <string>type</string>
                                <key>name</key>
                                <string>Sightseeing Tours</string>
                            </dict>
                        </dict>
                        <dict>
                            <key>product_category</key>
                            <dict>
                                <key>kind</key>
                                <string>category</string>
                                <key>name</key>
                                <string>Excursion</string>
                            </dict>
                        </dict>
                        <dict>
                            <key>product_category</key>
                            <dict>
                                <key>kind</key>
                                <string>type</string>
                                <key>name</key>
                                <string>Attractions</string>
                            </dict>
                        </dict>
                    </array>
                </dict>
            </dict>
            <dict>
                <key>result</key>
                <dict>
                    <key>description_short</key>
                    <string>The must see attractions of the United Kingdom, World Heritage sites, Stonehenge and the Roman Baths.</string>
                    <key>flex_reference</key>
                    <string>FLX-LON-65D-AC0-B08E</string>
                    <key>geocoded_latitude</key>
                    <string>51.5081289</string>
                    <key>geocoded_longitude</key>
                    <string>-0.128005</string>
                    <key>name</key>
                    <string>Stonehenge and Bath</string>
                    <key>primary_image</key>
                    <string>http://media.****.com/FLX-LON-65D-AC0-B08E-image_original-2E.jpg</string>
                    <key>product_categories</key>
                    <array>
                        <dict>
                            <key>product_category</key>
                            <dict>
                                <key>kind</key>
                                <string>type</string>
                                <key>name</key>
                                <string>Sightseeing Tours</string>
                            </dict>
                        </dict>
                        <dict>
                            <key>product_category</key>
                            <dict>
                                <key>kind</key>
                                <string>category</string>
                                <key>name</key>
                                <string>Excursion</string>
                            </dict>
                        </dict>
                    </array>
                </dict>
            </dict>
        </array>
        <key>total_pages</key>
        <integer>18</integer>
        <key>total_results</key>
        <integer>54</integer>
    </dict>
</dict>
</plist>

2 个答案:

答案 0 :(得分:1)

检索字典:

NSDictionary* dict= jsonDict[@"search_results"];

键“results”的值是一个数组:

NSArray* results = dict[@"results"];

在第一个索引处有一个字典:

NSDictionary* dict2= results[0];

对于关键的“结果”,还有另一个词典:

NSDictionary* dict3= dict2[@"result"];

您可以从此词典中检索所需的值:

NSString* name= dict3[@"name"];
NSString* primaryImage= dict3[@"primary_image"];

无论你有多少个dicts和数组,你都可以阅读一个,然后你可以全部阅读它们。为了做到这一点,只要想想它们就像数据包中的数据包一样,并用对象绘制图形。

编辑

我没有说明是什么,添加它。

答案 1 :(得分:1)

NSDictionary *jsonResponse = /* get your dictionary from wherever */
NSDictionary *searchResults = [jsonResponse objectForKey:@"search_result"];
NSArray *allResults = [searchResults objectForKey:@"results"];

for (NSDictionary *result in allResults)
{
    NSDictionary *resultDetails = [result objectForKey:@"result"];

    NSString *name = [resultDetails objectForKey:@"name"];
    NSURL *imageURL = [NSURL URLWithString:[resultDetails objectForKey:@"primary_image"]];

    NSLog(@"Name: %@\n URL: %@", name, imageURL);
}