2013-07-24 60 views
0

我想分析这样的json: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]); 

    } 
+0

json显示无效 –

+0

其有效..它与Android一起使用 –

+0

当且仅当密钥必须包含“”和“,”在末尾时才有效 –

回答

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); 

试试这个。 (未测试

+0

@ChitraKhatri URW :) –

0

只需说{}是一个字典和[]给你NSArray.So你有valueForKey:方法

所以

NSDictionary *dict = [jsonData objectForKey:@"-f1"] ; 
NSDictionary *photosDict = [dict objectForKey:@"Photos"] ; 
NSArray *weddingPhotos = [photosDict objectForKey:@"Wedding"] ; // array of dict 
到达品脱throught所有对象和数组