如何从NSDictionary获取字符串值?

时间:2012-05-23 10:00:11

标签: ios json sdk xcode4.2

在json解析后,我得到这样的字典值

 NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1
                                                         options:kNilOptions error:&error];

打印json词典后,我得到以下输出

{
       Title= "hi";
        title2 = "welcome";
        FriendlyName = B325;
        Id = 1;
        MyLocation = "opp";
        sliders =         (
                        {
                NAme1 = hai;
                Name2 = "apple";
                Name3 = "world";
                Name4 = "happiness";
            }
        );
        Address = "northZone";
        Title3 = "hello world";
    },
        {
       title= "hi";
        title2 = "welcome";
        FriendlyName = B325;
        Id = 2;
        MyLocation = "opp1";
        sliders =         (
                        {
                NAme1 = hai;
                Name2 = "apple";
                Name3 = "world";
                Name4 = "happiness";
            }
        );
        Address = "westZone";
        Title3 = "hello world";
    },

    title= "hi";
    title2 = "welcome";
    FriendlyName = B325;
    Id = 3;
    MyLocation = "opp";
    sliders =         (
                    {
            NAme1 = hai;
            Name2 = "apple";
            Name3 = "world";
            Name4 = "happiness";
        }
    );
    Address = "southZone";
    Title3 = "hello world";
},
 '''''''''etc.,


NSMutableArray *resultArray = [json objectForKey:@"title"]; 

我想将字典字符串存储到resultarray中,但执行已在上述步骤中停止

我如何将字典值存储到数组中?

请帮帮我

2 个答案:

答案 0 :(得分:0)

尝试使用此代码

NSMutableArray *resultArray = [[NSMutableArray alloc] init];
[resultArray addObject:[json objectForKey:@"title"]];

答案 1 :(得分:0)

从您显示的输出中,看起来您的JSON对象已经是一个字典数组。但是,你的陈述/问题对我来说没有意义,所以我无法弄清楚你真正想要的是什么。

有更多花哨的方式来访问您的数据,但这应该为您提供一个基本的想法,并希望让您顺利...

if ([json isKindOfClass:[NSArray class]]) {
    // Your top-object is an array of objects.  It can hold strings, numbers,
    // arrays, dictionaries (and NSNull).
} else if ([json isKindOfClass:[NSDictionary class]]) {
    // your top level object is a dictionary
}

从这里开始,您应该可以验证自己拥有的内容。我打赌你得到了一系列词典。此时,只需遍历顶级数组并查询每个字典的内容。

相关问题