从字典中访问json内容

时间:2013-03-21 09:54:13

标签: ios objective-c

我有一个包含Json数据的字典。我无法处理其中的数据。我尝试枚举,但一直都失败了。

This is the data format I am getting back using NSLog:

2013-03-21 12:48:36.973 SaleItems[21009:c07] {
    article = "Surface's other shoe is about to drop: the full Windows 8, Intel Core-driven Surface is headed to stores in just a few weeks.";
    id = 2;
    title = "Microsoft Surface Pro hits U.S. and Canada ";
}
2013-03-21 12:48:36.974 SaleItems[21009:c07] {
    article = "Hit by growing demand for Samsung devices, the iPhone will lose smartphone market share for the current quarter, says a Citi analyst";
    id = 3;
    title = "iPhone to shed market share";
}
2013-03-21 12:48:36.974 SaleItems[21009:c07] {
    article = "The carrier says it plans to buy wireless spectrum and network assets from Atlantic Tele-Network, aka Alltel, in an effort to boost its spectrum coffers.";
    id = 4;
    title = "AT&T spends $780 million ";
}

我从它的外观中知道我正在找回一个数组字典。如何访问其中的数组和元素?

2 个答案:

答案 0 :(得分:2)

看起来你有文章,id和标题的密钥,只需在循环中访问对象:

* 我假设您有一个名为SalesItem的类,上面的日志来自的每个对象

for(SalesItem *object in SalesItem){
    NSLog(@"Article : %@",object[@"article"]);
    NSLog(@"Id : %@",object[@"id"]);
    NSLog(@"Title : %@",object[@"title"]);
}

答案 1 :(得分:0)

这意味着您需要JSON解析。

您可以使用this链接向您展示如何在iOS5及更高版本中解析JSON。

一切顺利!!!