【问题标题】:Unable to parse only the imageURL from JSON in iOS无法在 iOS 中仅解析来自 JSON 的 imageURL
【发布时间】:2016-02-15 21:39:19
【问题描述】:
{
    "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。我已经创建了具有所有必需参数的模型对象。我能够解析和获取除 backgroundImageURL 之外的所有数据。我得到一个 NSUnknownKeyException。有人可以帮我解决这个问题吗?

以下是用于创建模型对象并将它们添加到数组中的代码。

 [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 核心基础 0x000000010e631e65 异常预处理 + 165 1 libobjc.A.dylib 0x000000010e0aadeb objc_exception_throw + 48 2 核心基础 0x000000010e631aa9 -[NSException raise] + 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 0x000000010f2335ba-[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 石英核心 0x000000011299be70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 13 石英核心 0x000000011299bcee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 14 石英核心 0x0000000112990475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 15 石英核心 0x00000001129bdc0a _ZN2CA11Transaction6commitEv + 486 16 石英核心 0x00000001129be37c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92 17 核心基础 0x000000010e55d367 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 18 核心基础 0x000000010e55d2d7 __CFRunLoopDoObservers + 391 19 核心基础 0x000000010e552f2b __CFRunLoopRun + 1147 20 核心基础 0x000000010e552828 CFRunLoopRunSpecific + 488 21 图形服务 0x000000011245cad2 GSEventRunModal + 161 22 UIKit 0x000000010e9de610 UIApplicationMain + 171 23 瓶火箭练习 0x000000010cd85f5f 主要 + 111 24 libdyld.dylib 0x00000001106f192d 开始 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止

【问题讨论】:

  • 显示你的完整代码和整个 json
  • 请显示此异常的完整堆栈跟踪 (NSUnknownKeyException)
  • 我们需要更多信息。显示您的餐厅课程和完整的 json
  • 我提供了更多信息。
  • 我想我知道出了什么问题。你能展示一下你是如何加载 json 的吗?

标签: ios objective-c


【解决方案1】:

我认为您的代码的问题在于您拥有一系列餐厅,而不仅仅是一家餐厅。您的 obj 没有钥匙,因为它有一系列餐馆。您只能获得每家餐厅的钥匙。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多