0

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"]; 

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

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

请帮我

4

2 回答 2

0

尝试使用此代码

NSMutableArray *resultArray = [[NSMutableArray alloc] init];
[resultArray addObject:[json objectForKey:@"title"]];
于 2012-05-23T10:16:07.663 回答
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
}

从这里,您应该能够验证您拥有什么。我敢打赌你会得到一系列字典。此时,只需遍历顶级数组并查询每个字典的内容。

于 2012-05-23T11:21:58.147 回答