JSON から取得した次の辞書をループするにはどうすればよいですか? ID 0001、0002 のみを取得するためにループするにはどうすればよいですか?
{
0001 = {
userName = "a";
photo = "";
};
0002 = {
userName = "b";
photo = "";
};
}
JSON から取得した次の辞書をループするにはどうすればよいですか? ID 0001、0002 のみを取得するためにループするにはどうすればよいですか?
{
0001 = {
userName = "a";
photo = "";
};
0002 = {
userName = "b";
photo = "";
};
}
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.
}
この方法を試してみてください...
すべてのキーを取得
NSArray *a=[yourDictionary allKeys];
もう 1 つの方法は、enumerateKeysAndObjectsUsingBlock:
API を使用してキーとオブジェクトを列挙することです。
使い方は至ってシンプルで、
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"Key: %@, Value:%@",key,obj);
if([key isEqualToString:@"0001"]) {
//Do something
}
// etc.
}];
それが役立つことを願っています!
NSArray *keys = [dictionary allKeys];
これを試して。配列内のすべてのキーを取得します。そして、それに応じてそれらを取得できますNSString
。
私は答えを見つけました。私はすでに次のコードを試しましたが、すべてのデータを提供しています。取得した json は wearg 形式であるためです。
for (NSString *key in Dict) {}