Objective c 从JSON生成的NSArray中选择

Objective c 从JSON生成的NSArray中选择,objective-c,multidimensional-array,Objective C,Multidimensional Array,我有一个从JSON文件创建的NSArray。 如果我只是将数组打印到控制台,我会得到以下输出: 文件的第一部分: { result = { "attribute_controlled_attached_particles" = ( { "attach_to_rootbone" = 0; id = 1; n

我有一个从JSON文件创建的NSArray。
如果我只是将数组打印到控制台,我会得到以下输出:

文件的第一部分:

{
    result =     {
        "attribute_controlled_attached_particles" =         (
                        {
                "attach_to_rootbone" = 0;
                id = 1;
                name = "Partikel 1";
                system = "burningplayer_red";
            },
                        {
                "attach_to_rootbone" = 0;
                id = 2;
                name = "Fliegende Teile";
                system = "burningplayer_flyingbits";
            },
                        {
                "attach_to_rootbone" = 0;
                attachment = muzzle1;
                id = 3;
                name = "Nemesis-Explosion";
                system = "particle_nemesis_burst_blue";
            },
                        {
                "attach_to_rootbone" = 0;
                id = 4;
                name = "Community-Glitzer";
                system = "community_sparkle";
            },
                        {
                "attach_to_rootbone" = 0;
                attachment = halopoint1;
                id = 5;
                name = Heiligenschein;
                system = halopoint;
            },
                        {
                "attach_to_rootbone" = 0;
                id = 6;
                name = "Gr\U00fcnes Konfetti";
                system = "superrare_confetti_green";
            },
                        {
                "attach_to_rootbone" = 0;
                id = 7;
                name = "Lila Konfetti";
                system = "superrare_confetti_purple";
            },
                        {
                "attach_to_rootbone" = 1;
                attachment = muzzle;
                id = 8;
                name = "Spukende Geister";
                system = "superrare_ghosts";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 9;
                name = "Gr\U00fcne Energieladung";
                system = "superrare_greenenergy";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 10;
                name = "Lila Energieladung";
                system = "superrare_purpleenergy";
            },
                        {
                "attach_to_rootbone" = 1;
                attachment = muzzle;
                id = 11;
                name = "Kreisendes TF-Logo";
                system = "superrare_circling_tf";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 12;
                name = Fliegenschwarm;
                system = "superrare_flies";
            },
                        {
                "attach_to_rootbone" = 1;
                attachment = muzzle;
                id = 13;
                name = "Brennende Flammen";
                system = "superrare_burning1";
            },
                        {
                "attach_to_rootbone" = 1;
                attachment = unusual;
                id = 14;
                name = "Sengende Flammen";
                system = "superrare_burning2";
            },
                        {
                "attach_to_rootbone" = 1;
                attachment = muzzle;
                id = 15;
                name = "Gl\U00fchendes Plasma";
                system = "superrare_plasma1";
            },
                        {
                "attach_to_rootbone" = 1;
                attachment = muzzle;
                id = 16;
                name = "Lebendiges Plasma";
                system = "superrare_plasma2";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 17;
                name = Sonnenstrahlen;
                system = "superrare_beams1";
            },
                        {
                "attach_to_rootbone" = 1;
                attachment = muzzle;
                id = 18;
                name = "Kreisendes Friedenszeichen";
                system = "superrare_circling_peacesign";
            },
                        {
                "attach_to_rootbone" = 1;
                attachment = muzzle;
                id = 19;
                name = "Kreisendes Herz";
                system = "superrare_circling_heart";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 20;
                name = "Karten-Briefmarken";
                system = "stamp_spin";
            },
                        {
                "attach_to_rootbone" = 0;
                attachment = "cig_drg_smoke";
                id = 28;
                name = "Eleganter Rauch";
                system = "drg_pipe_smoke";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 29;
                name = "St\U00fcrmischer Sturm";
                system = "unusual_storm";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 30;
                name = "Eisiger Sturm";
                system = "unusual_blizzard";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 31;
                name = "Muttern & Bolzen";
                system = "unusual_orbit_nutsnbolts";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 32;
                name = "Kreisende Planeten";
                system = "unusual_orbit_planets";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 33;
                name = "Kreisendes Feuer";
                system = "unusual_orbit_fire";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 34;
                name = Blubbern;
                system = "unusual_bubbles";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 35;
                name = Rauchen;
                system = "unusual_smoking";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 36;
                name = Dampfen;
                system = "unusual_steaming";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 37;
                name = "Brennende Laterne";
                system = "unusual_orbit_jack_flaming";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 38;
                name = "Bew\U00f6lkter Mond";
                system = "unusual_fullmoon_cloudy";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 39;
                name = "Blubbernder Kessel";
                system = "unusual_bubbles_green";
            },
                        {
                "attach_to_rootbone" = 1;
                id = 40;
                name = "Schauriges, kreisendes Feuer";
                system = "unusual_orbit_fire_dark";
            },
                        {
                "attach_to_rootbone" = 1;
                attachment = muzzle;
                id = 43;
                name = Messersturm;
                system = "unusual_storm_knives";
            },
                        {
                "attach_to_rootbone" = 1;
                attachment = muzzle;
                id = 44;
                name = "Nebliger Sch\U00e4del";
                system = "unusual_skull_misty";
            },
                        {
                "attach_to_rootbone" = 1;
                attachment = muzzle;
                id = 45;
                name = Herbstmond;
                system = "unusual_fullmoon_cloudy_green";
            },
                        {
                "attach_to_rootbone" = 1;
                attachment = muzzle;
                id = 46;
                name = "Es ist ein Geheimnis f\U00fcr jeden";
                system = "unusual_fullmoon_cloudy_secret";
            },
                        {
                "attach_to_rootbone" = 1;
                attachment = muzzle;
                id = 47;
                name = "St\U00fcrmische 13. Stunde";
                system = "unusual_storm_spooky";
            }
        );
        attributes =         (
                        {
                "attribute_class" = "mult_dmg";
                defindex = 1;
                "description_format" = "value_is_percentage";
                "description_string" = "%s1%\U00a0geringerer Schaden";
                "effect_type" = negative;
                hidden = 0;
                "max_value" = "0.85";
                "min_value" = "0.95";
                name = "damage penalty";
                "stored_as_integer" = 0;
            },
                        {
                "attribute_class" = "mult_dmg";
                defindex = 2;
                "description_format" = "value_is_percentage";
                "description_string" = "+%s1%\U00a0Schadensbonus";
                "effect_type" = positive;
                hidden = 0;
                "max_value" = "1.1";
                "min_value" = "1.05";
                name = "damage bonus";
                "stored_as_integer" = 0;
            },
                        {
                "attribute_class" = "mult_clipsize";
                defindex = 3;
                "description_format" = "value_is_percentage";
                "description_string" = "%s1%\U00a0Magazingr\U00f6\U00dfe";
                "effect_type" = negative;
                hidden = 0;
                "max_value" = "0.75";
                "min_value" = "0.9";
                name = "clip size penalty";
                "stored_as_integer" = 0;
            },
                        {
                "attribute_class" = "mult_clipsize";
                defindex = 4;
                "description_format" = "value_is_percentage";
                "description_string" = "+%s1%\U00a0Magazingr\U00f6\U00dfe";
                "effect_type" = positive;
                hidden = 0;
                "max_value" = "1.1";
                "min_value" = "1.05";
                name = "clip size bonus";
                "stored_as_integer" = 0;
            },
                        {
                "attribute_class" = "mult_postfiredelay";
                defindex = 5;
                "description_format" = "value_is_inverted_percentage";
                "description_string" = "%s1%\U00a0niedrigere Schussfrequenz";
                "effect_type" = negative;
                hidden = 0;
                "max_value" = "1.25";
                "min_value" = "1.05";
                name = "fire rate penalty";
                "stored_as_integer" = 0;
            },
                        {
                "attribute_class" = "mult_postfiredelay";
                defindex = 6;
                "description_format" = "value_is_inverted_percentage";
                "description_string" = "+%s1%\U00a0h\U00f6here Schussfrequenz";
                "effect_type" = positive;
                hidden = 0;
                "max_value" = "0.9";
                "min_value" = "0.95";
                name = "fire rate bonus";
                "stored_as_integer" = 0;
            },
                        {
                "attribute_class" = "mult_medigun_healrate";
                defindex = 7;
                "description_format" = "value_is_percentage";
                "description_string" = "%s1%\U00a0Heilungsrate";
                "effect_type" = negative;
                hidden = 0;
                "max_value" = "0.8";
                "min_value" = "0.95";
                name = "heal rate penalty";
                "stored_as_integer" = 0;
            },
                        {
                "attribute_class" = "mult_medigun_healrate";
                defindex = 8;
                "description_format" = "value_is_percentage";
                "description_string" = "+%s1%\U00a0Heilungsrate";
                "effect_type" = positive;
                hidden = 0;
                "max_value" = "1.15";
                "min_value" = "1.05";
                name = "heal rate bonus";
                "stored_as_integer" = 0;
            },
我的问题是:如何在索引中获取项的名称

我试过了

[[[[news objectAtIndex:0]objectForKey:@"items"]objectAtIndex:2] objectForKey:@"name"];
新闻是包含数据的数组

希望你能帮忙
蒂姆

你好像有一本由几组字典组成的字典。我想你想要这样的东西。。。(编辑:如注释中所述,这取决于您需要从哪个字典数组中获取名称,因为结果中似乎有几个字典可供选择)

或者当然:

NSString *someString = [[array objectAtIndex:3] objectForKey:@"name"];

您有一个包含另一个字典的字典(这不是一个聪明的想法),因此您应该使用objectForKey:selector将字典放入其中。现在,如果您能够获得字典的键值,那么N次都不会有问题

[]运算符允许您访问数组/字典的值,其工作原理类似于objectAtIndex:/valueForKey:。以下是如何做到这一点:

NSDictionary* result= json[@"result"];
NSArray* acabArray= result[@"attribute_controlled_attached_particles"];
for(NSDictionary* acabDict in acabArray)
{
    NSNumber* num= acabDict[@"attach_to_rootbone"];
    NSString* name= acabDict[@"name"];
    // Obtain also the other key-values in a similar way
    // Do what you want with these values, I suggest to build a custom
    // object to hold all these values, and put every object in a mutable array.
}

您有一个
NSDictionary
,其中包含一个
NSDictionary
(由“结果”键控),其中包含一个
NSArray
(由“属性控制的附加粒子”键控)。要获取此包含数组,您需要执行以下操作:

NSArray *resultArray = [[jsonDictionary objectForKey:@"result"] objectForKey:@"attribute_controlled_attached_particles"];
resultArray
中,您有一个表示新闻项的有序词典列表。因此,要获取第一个索引的“name”字符串,请执行以下操作

NSDictionary *itemDictionary = [resultArray objectAtIndex:0];
NSString *name = [itemDictionary objectForKey:@"name"];

要枚举所有项,您需要迭代resultArray。

我已下载了您的JSON文件,其中不包含任何键名为“items”的条目。“我有一个从JSON文件创建的NSArray”。不,您有一个字典,包含几个键…文件中有,但该文件是新闻的输出,新闻在我的标题中声明为nsarray(提示,当您转储JSON的Objective-C表示时,字典/对象用
{}
括起来,数组用
()
括起来。JSON数组中用
括起来[]
(另一个提示。Objective-C根本不关心您是否将对象称为NSArray,如果它实际上是NSDictionary(并且您可以将错误隐藏到编译器之前)。类型在运行时检测到,因此即使您的变量称为“NSArray”,它包含的指针也是“NSDictionary”。)很抱歉,您的第一个代码返回null,第二个代码给了我一个错误(我在我的原始帖子中添加了我的全部代码),在您显示的代码之外,我对您的代码没有详细的了解。如果您创建我在代码中使用的resultDictionary实例,那么它与您声明的“result=…”相同等等。我相信它应该工作得很好。
NSDictionary *itemDictionary = [resultArray objectAtIndex:0];
NSString *name = [itemDictionary objectForKey:@"name"];