如何从NSArray获取元素

时间:2012-02-07 11:29:56

标签: objective-c ios ios5 nsarray

我有一个来自twitter REST API的json数据构建的NSArray。

NSArray *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];

NSLog (@"Element %@", [dict objectAtIndex: 1]);

如何从数组中获取单个元素,其中一个数组索引项如下所示:

Element 1 = {
    contributors = "<null>";
    coordinates = "<null>";
    "created_at" = "Tue Feb 07 11:15:08 +0000 2012";
    entities =     {
        hashtags =         (
        );
        urls =         (
        );
        "user_mentions" =         (
        );
    };
    favorited = 0;
    geo = "<null>";
    id = 166842424398848000;
    "id_str" = 166842424398848000;
    "in_reply_to_screen_name" = "<null>";
    "in_reply_to_status_id" = "<null>";
    "in_reply_to_status_id_str" = "<null>";
    "in_reply_to_user_id" = "<null>";
    "in_reply_to_user_id_str" = "<null>";
    place = "<null>";
    "retweet_count" = 0;
    retweeted = 0;
    source = web;
    text = "another day is here so ready to be off from work already...";
    ....

2 个答案:

答案 0 :(得分:6)

你可以试试,

NSString *string = [[dict objectAtIndex: 1] objectForKey:@"keyName"];

如果我看起来正确(“实体”)

,数据中会有一个子词典
NSDictionary *entitiesDict = [[dict objectAtIndex: 1] objectForKey:@"entities"];

如上所述使用NSDictionary:)

答案 1 :(得分:0)

使用NSDictionary而不是NSArray并在key中设置元素的值。查看此链接以获取更多想法。

  

http://www.touch-code-magazine.com/tutorial-fetch-and-parse-json/