gpt4 book ai didi

objective-c - 无法解析json

转载 作者:行者123 更新时间:2023-12-01 18:30:45 25 4
gpt4 key购买 nike

我正在尝试解析Twitter返回的json。 json检索良好,我将其转换为NSDictionary,

NSLog在字典对象上运行良好,并显示所有推文。

NSLog(@"Twitter response: %@", dict);

但是当我尝试为dict对象中的任何键获取objectForKey时出现以下错误:

2012-02-07 15:35:28.988 TestTweetApp [1525:12103]-[__ NSCFArray
objectForKey:]:无法识别的选择器已发送到实例0x6a2a370
[切换到进程1525线程0x12103] [切换到进程1525
线程0x12103] 2012-02-07 15:35:28.997 TestTweetApp [1525:12103] *
由于未捕获的异常而终止应用程序
'NSInvalidArgumentException',原因:'-[__ NSCFArray objectForKey:]:
无法识别的选择器已发送到实例0x6a2a370'
*
第一个抛出调用堆栈:(0x176e052 0x1a40d0a 0x176fced 0x16d4f00 0x16d4ce2 0x3044 0xd306 0x2048445 0x2049ecf 0x2049d28 0x20494af
0x9ca1fb24 0x9ca216fe)终止,称为抛出
exceptionsharedlibrary apply-load-rules全部

viewDidLoad()函数的代码为:
- (void)viewDidLoad
{
[super viewDidLoad];

TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString: @"https://api.twitter.com/1/statuses/public_timeline.json?screen_name=[SOME_USER_NAME]&include_entities=true"] parameters:nil requestMethod:TWRequestMethodGET];

[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if ([urlResponse statusCode] == 200)
{
NSError *error;

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];

NSLog(@"Twitter response: %@", [dict objectForKey:@"entities"]);
}
else
NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]);
}];

}

最佳答案

看来您的回应是NSArray而不是NSDictionary。尝试

NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error]);

在你之前
NSDictionary *dict... 

行并查看格式。
然后将其移至NSArray并正确访问元素。
NSArray *response = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
NSLog(@"elements = %@", response);

关于objective-c - 无法解析json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9174879/

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