0

私はflickrで作業しており、サンプルフェッチでこれを取得します:

{
    "api_key" =     {
        "_content" = 3c6eeeae4711a5f478d3da796750e06b;
    };
    format =     {
        "_content" = json;
    };
    method =     {
        "_content" = "flickr.test.echo";
    };
    nojsoncallback =     {
        "_content" = 1;
    };
    stat = ok;
}

これは、5 つのエントリ (api_key、format、method、nojsoncallback & stat) を持つ辞書です。最初の 4 つの全体は辞書そのものです。

まず、元の辞書に 5 番目の要素がありますが、これは辞書ではありません。元の辞書の最後のエントリ (1 つの stat=ok) です。さらに、すべてのサブエントリの _content キーを個々のセルに表示したいのですが、値をハードコーディングしたくありません。アレイをセットアップする必要がありますか?

4

2 に答える 2

4

NSDictionary has a nifty little method called valueForKeyPath. Thats your savior here.

[dict valueForKeyPath:@"api_key._content"]
[dict valueForKeyPath:@"format._content"]
[dict valueForKeyPath:@"method._content"]
[dict valueForKeyPath:@"nojsoncallback._content"]

What it does is traverse the key path and fetch the values of content in each JSON substructure. Otherwise you would have had to written a for-loop and loop through it. Neat huh?

于 2013-04-16T04:11:32.620 に答える
2

これを試して

for (NSString *key in dictionary){
    id object = dictionary[key];
    if ([object isKindOfClass:[NSDictionary class]]) {
    //Now you can work on the dictionary object
    }
}
于 2013-04-16T04:11:41.597 に答える