-1

我想像这样解析json:

我想获取特定婚礼或仪式的 Photo_name。

我想保留婚礼的照片名称数组和仪式的其他照片名称数组。

{
 -f1: {
      Photos: {
          Wedding: [
                    {
                    Photo_id: "99",
                    Photo_name: "2WLB4cSJtg.jpg",
                    User_id: "11",
                    Date: "0000-00-00"
                    },
                    {
                    Photo_id: "97",
                    Photo_name: "EjOCbkWwmF.jpg",
                    User_id: "11",
                    Date: "0000-00-00"
                    },
                    {
                    Photo_id: "90",
                    Photo_name:"18e7af197f2060ad1c58d95c38f16ecc.jpg",
                    User_id: "11",
                    Date: "2013-07-24"
                    }
                  ],
        Ceremony: [
                   {
                    Photo_id: "96",
                    Photo_name: "hTWiAQUDnL.jpg",
                    User_id: "11",
                    Date: "0000-00-00"
                   },
                  {
                   Photo_id: "92",
                   Photo_name: "529753_425904027490910_737057237_n.jpg",
                   User_id: "11",
                   Date: "2013-07-24"
                  },
                  {
                   Photo_id: "93",
                   Photo_name: "yCbKG8txQY.jpg",
                   User_id: "11",
                   Date: "0000-00-00"
                  },
                  {
                   Photo_id: "94",
                   Photo_name: "k9goxnlscT.jpg",
                   User_id: "11",
                   Date: "0000-00-00"
                  },

                 ]
          }
      }
  }

此代码给我键 f1 并将 json 的所有内容显示为值:

 NSString *str=[[NSString alloc]initWithBytes:[webdata mutableBytes] length:[webdata length] encoding:NSUTF8StringEncoding];

   NSMutableDictionary *result = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
    for (id thekey in result) {
        NSLog(@"KEY %@ VALUE %@",thekey,[result valueForKey:thekey]);

     }
4

2 回答 2

1
NSString *str=[[NSString alloc]initWithBytes:[webdata mutableBytes] length:[webdata length] encoding:NSUTF8StringEncoding];

NSMutableDictionary *result = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];


NSArray *weddingPhotos = [[[[result valueForKey:@"f1"] valueForKey:@"Photos"] valueForKey:@"Wedding"] valueForKeyPath:@"Photo_name"];
NSLog(@"%@",weddingPhotos);

NSArray *ceremonyPhotos = [[[[result valueForKey:@"f1"] valueForKey:@"Photos"] valueForKey:@"Ceremony"] valueForKeyPath:@"Photo_name"];
NSLog(@"%@",ceremonyPhotos);

尝试这个。(未测试

于 2013-07-24T09:34:02.440 回答
0

valueForKey:简单地说 {} 是一个字典,而 [] 给你一个 NSArray。所以你必须通过方法到达所有对象和数组的品脱

所以

NSDictionary *dict = [jsonData objectForKey:@"-f1"] ;
NSDictionary *photosDict = [dict objectForKey:@"Photos"] ;
NSArray *weddingPhotos = [photosDict objectForKey:@"Wedding"] ; // array of dict
于 2013-07-24T09:35:46.530 回答