-2

JSON から取得した次の辞書をループするにはどうすればよいですか? ID 0001、0002 のみを取得するためにループするにはどうすればよいですか?

{
    0001 = {
      userName = "a";
      photo = "";
    };
    0002 = {
      userName = "b";
      photo = "";
    };
}
4

5 に答える 5

3

NSDictionaryキーをループします。

NSArray *keys = [dictionary allKey];

for (id *key in keys ) {
    NSDictionary *userPhoto = [dictionary objectForKey:key];
    // here you can either parse the object to a custom class
    // or just add it to an array.
}

または、高速列挙を直接使用しNSDictionaryます。

for (id *key in dictionary ) {
    NSDictionary *userPhoto = [dictionary objectForKey:key];
    // here you can either parse the object to a custom class
    // or just add it to an array.
}

キーごとにオブジェクトを取得できます。

またはenumerateKeysAndObjectsUsingBlock:

[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    // Here you can access the object and key directly.
}
于 2013-10-17T13:26:56.470 に答える
1

この方法を試してみてください...

すべてのキーを取得

NSArray *a=[yourDictionary allKeys];
于 2013-10-17T13:26:36.050 に答える
0

もう 1 つの方法は、enumerateKeysAndObjectsUsingBlock:API を使用してキーとオブジェクトを列挙することです。

使い方は至ってシンプルで、

[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    NSLog(@"Key: %@, Value:%@",key,obj);
    if([key isEqualToString:@"0001"]) {
        //Do something
    }
    // etc.
}];

それが役立つことを願っています!

于 2013-10-17T13:35:02.187 に答える
0
NSArray *keys = [dictionary allKeys];

これを試して。配列内のすべてのキーを取得します。そして、それに応じてそれらを取得できますNSString

于 2013-10-17T13:26:51.663 に答える
-1

私は答えを見つけました。私はすでに次のコードを試しましたが、すべてのデータを提供しています。取得した json は wearg 形式であるためです。

    for (NSString *key in Dict) {}  
于 2013-10-18T04:40:20.883 に答える