0

我在这里有点过头了(实际上很多)。我有一个已转换为 NSDictionary 的 JSON 响应。这是数组中的字典内的嵌套数组和字典的混乱,等等......我不知道如何处理它。我想做的是为每个“项目”制作一个简单的 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>
4

2 回答 2

1

检索字典:

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

键“结果”的值是一个数组:

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

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

NSDictionary* dict2= results[0];

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

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

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

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

无论你有多少字典和数组,你可以读取一个然后你可以读取它们。为了更容易做到这一点,只需将它们想象成数据包中的数据包,并用对象绘制图表。

编辑

我没有指定 dict 是什么,添加了它。

于 2012-12-17T02:31:58.127 回答
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);
}
于 2012-12-17T02:39:31.870 回答