我有一个简单的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