2011-09-24 100 views
0

我有從我的web服務器返回以下JSON對象。我無法弄清楚如何解析它...我基本上想要提取每個驅動程序的uname和profilePic(本例中有兩個驅動程序)。我對剩下的東西不感興趣。Objective-C:幫我解析這個JSON

這裏是JSON:

{ 
    "0": { 
     "uname": "Eamorr", 
     "fname": "Bill", 
     "lname": "Byrne", 
     "phoneNumber": "087-2342404", 
     "roofSignNumber": "32984", 
     "reputation": 0, 
     "vehicleMake": "Toyota", 
     "vehicleModel": "Carina", 
     "vehicleNumPassengers": "4", 
     "profilePic": "XE7F654O05RC3I33P44A", 
     "online": 1, 
     "lat": "52.27114461", 
     "lng": "-9.70294471", 
     "status": "done!", 
     "sex": "M", 
     "picList": ["W725CY1XR63PW480Z694", "XBA2078W4Z3JROQMSN16", "0H0XDD27J9J9RV28KKR4", "84G09NCP537G1KM6O4R8", "FM12F2J12AWL8C8XX2F5", "5AU4FLJ50PN0R210AP8J", "5588ORG95RF10B757NY7", "4z54cz5c565410r2wq2u", "3502IXTI31MSX6Z01NWC", "XE7F654O05RC3I33P44A", "A828K8M0E5576C1AK6HU", "M5EHX8MQMHZ6PJ1NNYH7", "8RIS3245542E2I9TLOD4", "3V5F7HNZNN642O29347Y", "YU2CT34A7769XG2LV38G"], 
     "last5comments": [{ 
      "comment": "asdf2", 
      "fromUname": "Anonymous", 
      "time": 1314036666 
     }, { 
      "comment": "qwerty", 
      "fromUname": "Eamorr", 
      "time": 1314550970 
     }, { 
      "comment": "qwerty", 
      "fromUname": "Eamorr", 
      "time": 1314551143 
     }, { 
      "comment": "hi", 
      "fromUname": "Eamorr2", 
      "time": 1315157494 
     }, { 
      "comment": "Hello", 
      "fromUname": "Anonymous", 
      "time": 1315394983 
     }], 
     "numPagesComments": 13 
    }, 
    "1": { 
     "uname": "Eamorr2", 
     "fname": "Steve", 
     "lname": "McCloskey", 
     "phoneNumber": "087-3234404", 
     "roofSignNumber": "32431", 
     "reputation": 0, 
     "vehicleMake": "Toyota", 
     "vehicleModel": "avensis", 
     "vehicleNumPassengers": "4", 
     "profilePic": -1, 
     "online": "0", 
     "lat": "52.28783634", 
     "lng": "-9.66791791", 
     "status": "", 
     "sex": "M", 
     "picList": [], 
     "last5comments": [{ 
      "comment": "asdf", 
      "fromUname": "Anonymous", 
      "time": 1296655686 
     }, { 
      "comment": "I'm off in the middle of the Atlantic again", 
      "fromUname": "Anonymous", 
      "time": 1296843759 
     }, { 
      "comment": "Hi", 
      "fromUname": "Eamorr", 
      "time": 1299098148 
     }, { 
      "comment": "Hi", 
      "fromUname": "Eamorr", 
      "time": 1299098148 
     }, { 
      "comment": "Hi", 
      "fromUname": "Eamorr", 
      "time": 1299098148 
     }], 
     "numPagesComments": 2 
    } 
} 

而且這裏是我試過的代碼了(沒有用...):

//NSDictionary *obj=[parser objectWithString:[request responseString] error:nil]; 
     NSArray *obj=[parser objectWithString:[request responseString] error:nil]; 

     NSLog([obj objectAtIndex:0]); 
     /*for(int i=0;i<obj.count;i++){ 
      NSDictionary *driver=[obj objectAtIndex:i]; 
      for (NSDictionary *dict in driver) { 
       [unames addObject:[dict objectForKey:@"uname"]]; 
       [profilePics addObject:[dict objectForKey:@"profilePic"]]; 
      } 
     } 

我希望有人可以提供幫助。


解決方案:

SBJsonParser *parser=[[SBJsonParser alloc]init]; 
     //NSDictionary *obj=[parser objectWithString:[request responseString] error:nil]; 
     NSDictionary *obj=[parser objectWithString:[request responseString] error:nil]; 

     for(int i=0;i<[obj count];i++){ 
      NSDictionary *obj2=[obj objectForKey:[NSString stringWithFormat:@"%d",i]]; 
      NSLog(@"%@",[obj2 objectForKey:@"uname"]); 
     } 
+1

什麼是你的 「分析器」 對象?什麼級別?你如何創建它? –

+0

對不起,我正在使用SBJSON – Eamorr

回答

1

使用SBJSON https://github.com/stig/json-framework

SBJSON *jsonReader = [[[SBJSON alloc] init] autorelease]; 
NSDictionary *responce = [jsonReader objectWithString:[request responseString]]; 

NSString *uName1 = [[responce objectForKey:@"0"] objectForKey:@"uname"]; 
NSString *image1 = [[responce objectForKey:@"0"] objectForKey:@"profilePic"]; 
+0

嘿,我正在使用SBJSON。問題是這兩個驅動程序是在一個數組中,而不是一個NSDictionary – Eamorr

+1

他們不是,根據你上面發佈的表示,他們是NSDictionaries和@Vladislav發佈的解決方案是正確的。 – Rog

+0

它們不在Array中。他們在一個詞典。 –