【问题标题】:how to fetch value of all source url in array from given string如何从给定字符串中获取数组中所有源url的值
【发布时间】:2012-07-09 09:26:17
【问题描述】:
{ 数据 = ( { "created_time" = "2011-09-28T07:29:37+0000"; 从 = { ID = 100002944043966; name = "alok sinha"; }; 高度 = 500; 图标=“https://s-static.ak.facebook.com/rsrc.php/v2/yz/r/StEh3RhPvjk.gif”; ID = 114750595299741; 图片 = ( { 高度 = 2048; 来源=“https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s2048x2048/300035_114750595299741_1543248164_n.jpg”; 宽度 = 2048; }, { 高度 = 500; 来源=“https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_n.jpg”; 宽度 = 500; }, { 高度 = 500; 来源=“https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_n.jpg”; 宽度 = 500; }, { 高度 = 480; 来源=“https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s480x480/300035_114750595299741_1543248164_n.jpg”; 宽度 = 480; }, { 高度 = 320; 来源=“https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s320x320/300035_114750595299741_1543248164_n.jpg”; 宽度 = 320; }, { 高度 = 180; 来源=“https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_a.jpg”; 宽度 = 180; }, { 高度 = 130; 来源=“https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_s.jpg”; 宽度 = 130; }, { 高度 = 130; 来源=“https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/s75x225/300035_114750595299741_1543248164_s.jpg”; 宽度 = 130; } ); 链接=“https://www.facebook.com/photo.php?fbid=114750595299741&set=a.114750591966408.20279.100002944043966&type=1”; 图片=“https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_s.jpg”; 位置 = 1; 来源=“https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_n.jpg”; "updated_time" = "2011-09-28T07:29:38+0000"; 宽度 = 500; } ); 分页 = { 下一个 = "https://graph.facebook.com/114750591966408/photos?value=1&redirect=1&limit=25&after=MTE0NzUwNTk1Mjk5NzQx"; }; }

【问题讨论】:

  • 您是否使用 SBJSON 或任何其他替代方法将 JSON 解析为对象表示?
  • 您的第一步是获取有效的 JSON...() 无效。等号无效,分号无效......这甚至是JSON吗?
  • 这似乎是解析后的 JSON 打印到 NSLog 中 :)
  • @Eugene Right,现在我需要获取所有源 url!

标签: iphone objective-c ios


【解决方案1】:
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。

【讨论】:

    【解决方案2】:

    你的图片数组只是[jsonObject objectForKey:@"images"],所以遍历所有这些图片并获取[loopObject objectForKey:@"source"],最后,不要忘记[jsonObject objectForKey:@"source"]

    【讨论】:

      【解决方案3】:

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

      我认为这对你会有帮助。

      【讨论】:

      • 数组第一个索引处存在的所有值!没有键!
      • 我不明白你的意思。
      • 在您的数据中,图像是关键,而相应的关键(图像)的值在那里。
      • 但是当我尝试获取对应于图像键的值时,我得到了 null!你有任何..
      • NSLog(@"%@",[dictionary objectForKey:@"images"]);喜欢就试试吧。
      【解决方案4】:

      在您的代码中包含 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"]];
      }
      

      【讨论】:

      • -1 用于 SBJSON。如果您正在为 iOS 5+ 进行开发,请使用内置的 NSJSONSerialization。如果没有,请使用 JSONKit……它更小更快。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多