如何从给定的字符串中获取数组中所有源url的值

时间:2012-07-09 09:26:17

标签: iphone objective-c ios

{
    data =     (
                {
            "created_time" = "2011-09-28T07:29:37+0000";
            from =             {
                id = 100002944043966;
                name = "alok sinha";
            };
            height = 500;
            icon = "https://s-static.ak.facebook.com/rsrc.php/v2/yz/r/StEh3RhPvjk.gif";
            id = 114750595299741;
            images =             (
                                {
                    height = 2048;
                    source = "https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s2048x2048/300035_114750595299741_1543248164_n.jpg";
                    width = 2048;
                },
                                {
                    height = 500;
                    source = "https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_n.jpg";
                    width = 500;
                },
                                {
                    height = 500;
                    source = "https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_n.jpg";
                    width = 500;
                },
                                {
                    height = 480;
                    source = "https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s480x480/300035_114750595299741_1543248164_n.jpg";
                    width = 480;
                },
                                {
                    height = 320;
                    source = "https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s320x320/300035_114750595299741_1543248164_n.jpg";
                    width = 320;
                },
                                {
                    height = 180;
                    source = "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_a.jpg";
                    width = 180;
                },
                                {
                    height = 130;
                    source = "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_s.jpg";
                    width = 130;
                },
                                {
                    height = 130;
                    source = "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/s75x225/300035_114750595299741_1543248164_s.jpg";
                    width = 130;
                }
            );
            link = "https://www.facebook.com/photo.php?fbid=114750595299741&set=a.114750591966408.20279.100002944043966&type=1";
            picture = "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_s.jpg";
            position = 1;
            source = "https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_n.jpg";
            "updated_time" = "2011-09-28T07:29:38+0000";
            width = 500;
        }
    );
    paging =     {
        next = "https://graph.facebook.com/114750591966408/photos?value=1&redirect=1&limit=25&after=MTE0NzUwNTk1Mjk5NzQx";
    };
}

4 个答案:

答案 0 :(得分:2)

NSArray *jsonArray; // your parsed array

NSDictionary *dict = [jsonArray objectAtIndex:0]; // there might be more of the objects and you might need to put that into a forin cycle, but for simplicity this is an example for an array with only one dictionary in it

NSArray *images = [dict objectForKey:@"images"];

NSMutableArray *links = [NSMutableArray array];

for (NSDictionary *img in images) {
  [links addObject:[img valueForKey:@"source"]];
}

现在,链接是您需要的NSString对象数组。如果需要,您也可以将它们转换为NSURL。

答案 1 :(得分:2)

您的图片数组只是[jsonObject objectForKey:@"images"]所以请循环浏览所有图片,然后选择[loopObject objectForKey:@"source"],最后不要忘记[jsonObject objectForKey:@"source"]

答案 2 :(得分:1)

[yourDictionary objectForKey:@"data"]提供字典。

NSDictionary *dictionary = [yourDictionary objectForKey:@"data"]

    NSArray *images = [dictionary objectForKey:@"images"];

          NSMutableArray *sourceUrls = [[NSMutableArray alloc] initWithCapacity:0];

            for (NSDictionary *subDictionary in images]) {

                    [sourceUrls addObject:[subDictioanary objectForKey:@"source"]];
                }

我认为这会对你有所帮助。

答案 3 :(得分:-1)

在代码中包含SBJSON解析器。然后在实现文件中导入SBJson.h。然后使用以下代码。

NSMutableDictionary *responseDictionary = [yourJsonString JSONValue];
NSArray *imageArray = [responseDictionary objectForKey:@"images"];

NSMutableArray *sourceImage=[[NSMutableArray alloc]init]
for(int i=0;i<[imageArray count];i++){
{
[sourceImage appendString:[[imageArray objectAtIndex:i]objectForKey:@"source"]];
}