我有一個簡單的NSDictionary
,我試圖通過返回的JSON從外部網站的數據填充。返回的JSON沒問題,但我無法獲取特定鍵的實際數據。JSON解析與NSDictionary和鍵值查找值
這是打印到控制檯的JSON數據。
這是我的JSON數據:
(
{
CategoryID = 12345;
CategoryName = "Baked Goods";
},
{
CategoryID = 12346;
CategoryName = Beverages;
},
{
CategoryID = 12347;
CategoryName = "Dried Goods";
},
{
CategoryID = 12348;
CategoryName = "Frozen Fruit & Vegetables";
},
{
CategoryID = 12349;
CategoryName = Fruit;
},
{
CategoryID = 12340;
CategoryName = "Purees & Soups";
},
{
CategoryID = 12341;
CategoryName = Salad;
},
{
CategoryID = 12342;
CategoryName = "Snack Items";
},
{
CategoryID = 12343;
CategoryName = Vegetables;
}
)
我得到的錯誤是:
終止應用程序由於未捕獲的異常 'NSInvalidArgumentException' 的,理由是:「 - [__ NSCFArray enumerateKeysAndObjectsUsingBlock: ]:無法識別的選擇發送到 實例0x6884000'
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSError *error = nil;
// Get the JSON data from the website
NSDictionary *categories = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (categories.count > 0){
NSLog(@"This is my JSON data %@", categories);
[categories enumerateKeysAndObjectsUsingBlock: ^(__strong id key, __strong id obj, BOOL *stop) {
NSLog(@"Key = %@, Object For Key = %@", key, obj); }];
}
我不知道爲什麼會發生這種情況,但我確定這很簡單,就像我使用不正確的對象或其他東西。
幫助表示讚賞。
嘗試從錯誤中解決問題。它會告訴你你需要的一切。 – hooleyhoop