2

私はこのコードを持っており、キー値nombreを取得して NSArray *array に入力する必要がありますが、機能しません

NSURL *urlPaises = [NSURL URLWithString:@"http://tr.com.mx/prb2/buscarPais.php"];
NSData *dataPaises = [NSData dataWithContentsOfURL:urlPaises];

    NSArray *array;

    NSMutableDictionary *jsonPaises;
    NSError *error;

    array = [[NSArray alloc]init];


        jsonPaises = [NSJSONSerialization JSONObjectWithData:dataPaises options:kNilOptions error:&error];

        array = [jsonPaises objectForKey: @"nombre"];


        Printing description of self->jsonPaises:
        {
            paises =     (
                        {
                    id = 49;
                    nombre = Alemania;
                },
                        {
                    id = 54;
                    nombre = Argentina;
                },

                        {
                    id = 44;
                    nombre = Inglaterra;
                },

                        {
                    id = 598;
                    nombre = Uruguay;
                },
                        {
                    id = 58;
                    nombre = Venezuela;
                }
            );
        }
4

3 に答える 3

3

使用する必要があるようですvalueForKeyPath

[jsonPaises valueForKeyPath:@"praises.nombre"]

の配列を返す必要がありますnombres

于 2013-10-19T16:12:52.080 に答える
0

jsonPaises配列です。配列に0,1,2はキーではなくインデックスがあります"apple","foo","bar"。キーの値を配列に問い合わせても、うまくいきません。辞書の配列があります。元の配列のすべての辞書のキー「nombre」の値を持つ新しい配列が必要な場合は、これを試してください。

NSMutableArray * newArray = [[NSMutableArray alloc]init]; //create the new array
for (NSDictionary * dict in array) {  //for each dictionary in the first array
    id object = dict[@"nombre"]; //get the object stored in the key
    if(object) { //check that the key/value was actually in the dict
         [newArray addObject:object]; //add the object to the new Array
    }
}
于 2013-10-19T16:12:43.067 に答える
0

キー @"nombre" の値が単一の名前として来るのか、それとも名前の配列なのか、より具体的にする必要がありますか? 私はそれが名前の配列であると推測し、試してみます:

for (NSString *name in [jsonPaises objectForKey:@"nombre"]) {
      NSLog("%@", name);
}

キー @"nombre" の値が単一の文字列の場合、次のように配列に追加します。

[array addObject:[jsonPaises objectForKey: @"nombre"]];
于 2013-10-19T16:12:58.447 に答える