ios - 使用 NSJSONSerialization 解析 json 结果

标签 ios objective-c json

我正在尝试解析这个 json:

{
    "myData": [
        {
            "date": "2013-07-29",
            "preferredMeetingLocation": "home",
            "isbn": null,
            "category": "Clothing",
            "price": "5",
            "title": "clothingstuff",
            "description": "Desc"
        },
        {
            "date": "2013-07-29",
            "preferredMeetingLocation": "home2",
            "isbn": null,
            "category": "Clothing",
            "price": "2",
            "title": "other",
            "description": "Desc2"
        }
    ]
}

到目前为止我有:

    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
    NSDictionary *results = [json objectForKey:@"myData"];
for (NSDictionary *item in results) {
    NSLog(@"results::%@", [results objectForKey:@"title"]);
}

但我得到 Termating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8877e40'

复制代码

主要目标是能够解析接收到的数据,然后在单元格中显示每组信息。

我做错了什么?

最佳答案

线

 NSLog(@"results::%@", [results objectForKey:@"title"]);
 //                        ^---- Wrong variable used here!

应该是

 NSLog(@"results::%@", [item objectForKey:@"title"]);

关于ios - 使用 NSJSONSerialization 解析 json 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17931243/

相关文章:

ios - 在 UIsplitView 中更改主按钮的标题

ios - SocketRocket 在尝试安装 socketrocket 以便我可以使 PonyDebugger 工作之后,我收到 3 个 Apple Match-O 链接器错误

ios - 企业 ios 应用程序中的崩溃日志

c# - Newtonsoft.Json.Schema.JsonSchema 已过时?

ios - 如何在不使用临时文件的情况下在 UIWebview 中打开受密码保护的 PDF/DOC?

ios - CloudKit CK订阅,订阅ID

objective-c - 发生viewdidappear后加载uitableview数据?

objective-c - 附加 CString 是否比附加 ObjC 字符串更好?

javascript - 有没有办法使用 jquery 解析这个 json

c# - 使用 C# 查询 MongoDB 嵌套数组文档