2012-02-09 43 views
0

我有这样一个NSDictionary。NSDictionary与字典作为对象,获取对象

PhotoData =  { 
    1 =   { 
     "jf_photo_geotag" = "arayat philippines"; 
     "jf_photo_id" = 1; 
     "jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage1.png"; 
     "jf_user_id" = 1; 
     comments =    { 
      1 =     { 
       "jf_photo_comment" = "well i like this one. really. comment from janus"; 
       "jf_photo_comment_id" = 1; 
       "jf_photo_id" = 1; 
       "jf_user_id" = 1; 
       "jf_username" = janus; 
      }; 
      4 =     { 
       "jf_photo_comment" = asasfa; 
       "jf_photo_comment_id" = 4; 
       "jf_photo_id" = 1; 
       "jf_user_id" = 1; 
       "jf_username" = janus; 
      }; 
     }; 
     likes =    { 
      2 =     { 
       "jf_like_id" = 2; 
       "jf_photo_id" = 1; 
       "jf_user_id" = 1; 
       "jf_username" = janus; 
      }; 
     }; 
     "post_date" = "2012-02-23 16:34:14"; 
    }; 
    2 =   { 
     "jf_photo_geotag" = "magalang philippines"; 
     "jf_photo_id" = 2; 
     "jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage2.png"; 
     "jf_user_id" = 2; 
     comments =    { 
      2 =     { 
       "jf_photo_comment" = "well this is now a comment from loaclhost! for phot number 2"; 
       "jf_photo_comment_id" = 2; 
       "jf_photo_id" = 2; 
       "jf_user_id" = 2; 
       "jf_username" = fidel; 
      }; 
      3 =     { 
       "jf_photo_comment" = asdfadf; 
       "jf_photo_comment_id" = 3; 
       "jf_photo_id" = 2; 
       "jf_user_id" = 2; 
       "jf_username" = fidel; 
      }; 
     }; 
     likes =    { 
      1 =     { 
       "jf_like_id" = 1; 
       "jf_photo_id" = 2; 
       "jf_user_id" = 1; 
       "jf_username" = janus; 
      }; 
     }; 
     "post_date" = "2012-02-23 16:34:31"; 
    }; 
    3 =   { 
     "jf_photo_geotag" = "san fdo philippines"; 
     "jf_photo_id" = 3; 
     "jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage1.png"; 
     "jf_user_id" = 3; 
     comments =    (
     ); 
     likes =    (
     ); 
     "post_date" = "2012-02-24 16:34:47"; 
    }; 
    }; 
success = 1; 

}

基本上,这NSDictionary中包含像jf_photo_url,jf_photo_id等一些字符串数据,也有另一种解释,其目的在于这样的评论=()

我怎么能这个工作字典改变其结构和生产,并可能创建一个新的解释与此类似:

PhotoData =  { 
    { 
     "jf_photo_geotag" = "arayat philippines"; 
     "jf_photo_id" = 1; 
     "jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage1.png"; 
     "jf_user_id" = 1; 
     comments = (
      { 
       "jf_photo_comment" = "well i like this one. really. comment from janus"; 
       "jf_photo_comment_id" = 1; 
       "jf_photo_id" = 1; 
       "jf_user_id" = 1; 
       "jf_username" = janus; 
      }, 
      { 
       "jf_photo_comment" = asasfa; 
       "jf_photo_comment_id" = 4; 
       "jf_photo_id" = 1; 
       "jf_user_id" = 1; 
       "jf_username" = janus; 
      } 
     }; 
     likes = (
       { 
       "jf_like_id" = 2; 
       "jf_photo_id" = 1; 
       "jf_user_id" = 1; 
       "jf_username" = janus; 
      } 
     ,"post_date" = "2012-02-23 16:34:14"; 
    }, 

    { 
     "jf_photo_geotag" = "magalang philippines"; 
     "jf_photo_id" = 2; 
     "jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage2.png"; 
     "jf_user_id" = 2; 
     comments =(
       { 
       "jf_photo_comment" = "well this is now a comment from loaclhost! for phot number 2"; 
       "jf_photo_comment_id" = 2; 
       "jf_photo_id" = 2; 
       "jf_user_id" = 2; 
       "jf_username" = fidel; 
      }, 
      { 
       "jf_photo_comment" = asdfadf; 
       "jf_photo_comment_id" = 3; 
       "jf_photo_id" = 2; 
       "jf_user_id" = 2; 
       "jf_username" = fidel; 
      }; 
     }; 
     likes = (
      { 
       "jf_like_id" = 1; 
       "jf_photo_id" = 2; 
       "jf_user_id" = 1; 
       "jf_username" = janus; 
      }; 
     }, 
     "post_date" = "2012-02-23 16:34:31"; 
    }, 
    { 
     "jf_photo_geotag" = "san fdo philippines"; 
     "jf_photo_id" = 3; 
     "jf_photo_url" = "http://adomainname/iphone_images/large/jf_largeimage1.png"; 
     "jf_user_id" = 3; 
     comments =    (
     ); 
     likes =    (
     ); 
     "post_date" = "2012-02-24 16:34:47"; 
    }; 
    }; 
success = 1; 

}

注意到第一个字典在每个对象中都有索引号。我想删除它们。 我的第一本字典是从服务器上加载的,第二个是来自plist

谢谢你们!我希望你能理解我的问题。

回答

2

它看起来像你希望你的外部字典是一个数组。假设你已经有NSDictionary中的这些数据,你可以像下面这样拷贝它到一个数组中:

NSDictionary* photoDict = some_dict; //your outer dict 
NSMutableArray* photoArray = [[[NSMutableArray alloc] init] autorelease]; 

NSEnumerator *enumerator = [photoDict objectEnumerator]; 
id value; 
while((value = [enumerator nextObject])) { 
    [photoArray addObject:value]; 
} 

//do something with photoArray 
+0

hi @badger!是的... ..会尝试你的代码片段,并让你知道!谢谢! – janusbalatbat 2012-02-09 11:05:33