2016-02-15 69 views
0
{ 
    "restaurants" : [ 
    { 
     "name": "Hopdoddy Burger Bar", 
     "backgroundImageURL": "http://sandbox.bottlerocketapps.com/BR_iOS_CodingExam_2015_Server/Images/hopdoddy.png", 
     "category" : "Burgers", 
      "contact": { 
       "phone": "9723872337", 
       "formattedPhone": "(972) 387-2337", 
       "twitter": "hopdoddy" 
      }, 
      "location": { 
       "address": "5100 Belt Line Road, STE 502", 
       "crossStreet": "Dallas North Tollway", 
       "lat": 32.950787, 
       "lng": -96.821118, 
       "postalCode": "75254", 
       "cc": "US", 
       "city": "Addison", 
       "state": "TX", 
       "country": "United States", 
       "formattedAddress": [ 
        "5100 Belt Line Road, STE 502 (Dallas North Tollway)", 
        "Addison, TX 75254", 
        "United States" 
       ] 
      } 
    }, 
    { 
     "name": "Pappadeaux Seafood Kitchen", 
     "backgroundImageURL": "http://sandbox.bottlerocketapps.com/BR_iOS_CodingExam_2015_Server/Images/pappadeaux.png", 
     "category": "Seafood", 
      "contact": { 
       "phone": "9724479616", 
       "formattedPhone": "(972) 447-9616", 
       "twitter": "pappadeaux" 
      }, 
      "location": { 
       "address": "18349 Dallas Pkwy", 
       "crossStreet": "at Frankford Rd.", 
       "lat": 32.99908456526653, 
       "lng": -96.83018780592823, 
       "postalCode": "75287", 
       "cc": "US", 
       "city": "Dallas", 
       "state": "TX", 
       "country": "United States", 
       "formattedAddress": [ 
        "18349 Dallas Pkwy (at Frankford Rd.)", 
        "Dallas, TX 75287", 
        "United States" 
       ] 
      } 
    }, 
{ 
     "name": "Buffalo Wild Wings", 
     "backgroundImageURL": "http://sandbox.bottlerocketapps.com/BR_iOS_CodingExam_2015_Server/Images/buffalo_wild_wings.png", 
     "category": "Wing Joint", 
      "contact": { 
       "phone": "9727019464", 
       "formattedPhone": "(972) 701-9464", 
       "twitter": "bwwings" 
      }, 
      "location": { 
       "address": "5000 Belt Line Rd Ste 100", 
       "crossStreet": "at Quorum Dr", 
       "lat": 32.95347617827522, 
       "lng": -96.82554602622986, 
       "postalCode": "75254-6752", 
       "cc": "US", 
       "city": "Dallas", 
       "state": "TX", 
       "country": "United States", 
       "formattedAddress": [ 
        "5000 Belt Line Rd Ste 100 (at Quorum Dr)", 
        "Dallas, TX 75254-6752", 
        "United States" 
       ] 
      } 
    } 
    ] 
} 

這是我試圖在iOS中解析的JSON。我用所有必需的參數創建了我的Model對象。我能夠解析並獲取除backgroundImageURL之外的所有數據。我得到一個NSUnknownKeyException。有人可以幫我弄這個嗎?無法僅從iOS中的JSON解析imageURL

下面是用於創建模型對象並將它們添加到數組的代碼。

[self.restaurantsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 

     Restaurant *restaurant = [[Restaurant alloc]init]; 
     restaurant.name = [obj objectForKey:@"name"]; 
     restaurant.imageURL = [obj objectForKey:@"backgroundImageURL"]; 
     restaurant.category = [obj objectForKey:@"category"]; 
     restaurant.contact = [obj objectForKey:@"contact"]; 
     restaurant.location = [obj objectForKey:@"location"]; 
     [self.parsedRestaurantArray addObject:restaurant]; 

    }]; 

這是餐廳類:

@interface Restaurant : NSObject 

@property (nonatomic, strong)NSString *name; 
@property (nonatomic, strong)NSString *imageURL; 
@property (nonatomic, strong)NSString *category; 
@property (nonatomic, strong)Contact *contact; 
@property (nonatomic, strong)Location *location; 

@end 

這裏是軌道跟蹤:

終止應用程序由於未捕獲的異常 'NSUnknownKeyException',原因:「[valueForUndefinedKey:]:此類不是關鍵值backgroundImageURL的關鍵值編碼。「 ***第一擲調用堆棧: ( 0的CoreFoundation 0x000000010e631e65 exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010e0aadeb objc_exception_throw + 48 2的CoreFoundation 0x000000010e631aa9 - [NSException提高] + 9 3基礎0x000000010dd0a888 - [NSObject的(的NSKeyValueCoding )valueForUndefinedKey:] + 226 4基金會0x000000010dc60997 - [NSObject的(的NSKeyValueCoding)valueForKey:] + 280 5基金會0x000000010dc60758 - [NSArray的(的NSKeyValueCoding)valueForKey:] + 437 6 BottleRocketExercise 0x000000010cd8692a - [LunchesViewController的CollectionView:cellForItemAtIndexPath:] + 282 7 UIKit 0x0000000 10f2335ba - [UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:] + 483 8的UIKit 0x000000010f235ae0 - [UICollectionView _updateVisibleCellsNow:] + 4431 9的UIKit 0x000000010f23a23b - [UICollectionView layoutSubviews] + 247 10的UIKit 0x000000010ea954a3 - [UIView的(CALayerDelegate)layoutSublayersOfLayer: ] + 703 11 QuartzCore 0x00000001129a759a - [CALayer的layoutSublayers] + 146 12 QuartzCore 0x000000011299be70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 13 QuartzCore 0x000000011299bcee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 14 QuartzCore 0x0000000112990475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 15 QuartzCore 0x00000001129bdc0a _ZN2CA11Transaction6commitEv + 486 16 QuartzCore 0x00000001129be37c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92 17的CoreFoundation 0x000000010e55d367 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 18的CoreFoundation 0x000000010e55d2d7 __CFRunLoopDoObservers + 391 19的CoreFoundation 0x000000010e552f2b __CFRunLoopRun + 1147 20的CoreFoundation 0x000000010e552828 CFRunLoopRunSpecific + 488個 21個GraphicsServices 0x000000011245cad2 GSEventRunModal + 161 22 UIKit 0x000000010e9de610 UIApplicationMain + 171 23 BottleRocketExercise 0x000000010cd85f5f main + 111 24 libdyld.dylib 0x00000001106f192d啓動+ 1 ) 的libC++ abi.dylib:與類型NSException的未捕獲的異常終止

+0

顯示您的完整代碼和整個json – hariszaman

+0

請顯示此異常的完整堆棧跟蹤(NSUnknownKeyException) – shpasta

+0

我們需要更多信息。展示您的餐廳類和完整的json –

回答

1

我覺得你的代碼的問題是,你有和各類餐廳,而不僅僅是一個餐廳。你的obj沒有鑰匙,因爲它有一些餐館。你只能得到每家餐廳的鑰匙。

+0

喬納斯你的答案幫助..謝謝.. – user1342592