gpt4 book ai didi

ios - objective-c/JSON : How to get ObjectForKey when it's a top level object

转载 作者:行者123 更新时间:2023-11-29 13:05:59 24 4
gpt4 key购买 nike

我的 JSON 看起来像这样:

[
{
post_number: "1",
date: "2013-09-02",
thumbnail: "thumbnail_address"
},
{
post_number: "2",
date: "2013-09-02",
thumbnail: "thumbnail_address"
},
{
post_number: "2",
date: "2013-09-02",
thumbnail: "thumbnail_address"
},
]

我通常做这样的事情 NSArray *blogPostsArray = [dataDictionary objectForKey:@"posts"]; 但没有一个名为“posts”的顶级对象。当 Objective -c 中没有顶级对象时,如何获取数组?

提前谢谢你..

最佳答案

如果您的顶级对象是数组而不是字典,NSJSONSerialization 将创建一个 NSArray 实例而不是 NSDictionary
因此,您可以通过 objectAtIndex:(或通过现代 Objective-C 中的 [])检索包含的对象:

NSArray* jsonArray = @[@{
@"post_number": @"1",
@"date": @"2013-09-02",
@"thumbnail": @"thumbnail_address"
},
@{
@"post_number": @"2",
@"date": @"2013-09-02",
@"thumbnail": @"thumbnail_address"
},
@{
@"post_number": @"2",
@"date": @"2013-09-02",
@"thumbnail": @"thumbnail_address"
}];
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonArray options:0 error:nil];
NSArray* deserializedArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
NSDictionary* first = deserializedArray[0];
NSLog(@"%@", first);

关于ios - objective-c/JSON : How to get ObjectForKey when it's a top level object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18628465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com