gpt4 book ai didi

ios - 获取NSDictionary值 objective-c

转载 作者:行者123 更新时间:2023-12-01 17:50:22 24 4
gpt4 key购买 nike

我的Nsdictionary看起来像这样。

{
thread = "{
\"uuid\": \"e9290344dee38c42782\",
\"site_full\": \"www.independent.co.uk\",
\"main_image\":\"http://www.independent.co.uk/incoming/article10498560.ece/binary/original/39-tea-party-ap.jpg\"
}";
}

[newDict valueForKeyPath:@"thread.main_image"];

我试图像这样访问它,但它可以获取线程外的所有其他值,但不能嵌套线程中的嵌套值。

编辑
      for (int i=0;i < [responseObject count];i++) {
NSDictionary* newDict = (NSDictionary*)responseObject[i];

if ([(NSString*)[newDict valueForKey:@"title"] isEqualToString: @""] || [newDict valueForKey:@"title"]== Nil) {
NSLog(@"Image or Title Text is null");
}
else{
NewsFeed* newsData = [NewsFeed new];
newsData.newsSource = [newDict valueForKeyPath:@"thread.site"];
newsData.newsImageLink = [newDict valueForKeyPath:@"thread.main_image"];
//these three work fine
newsData.newsTitle = [newDict valueForKeyPath:@"title"];
newsData.newsText = [newDict valueForKeyPath:@"text"];
newsData.newsDate = [newDict valueForKeyPath:@"published"];
//
NSLog(@"news Source :: %@ \n and news ImageLink:: %@ \n news Title %@ \nnews Text:: %@ \n news Date:: %@ ",newsData.newsSource,newsData.newsImageLink,newsData.newsTitle,newsData.newsText,newsData.newsDate);
[NewsData addObject:newsData];
}


}

最佳答案

看起来存储在索引下标"thread"上的对象不是假定的NSDictionary,而是看起来是NSString的实例。

这很微妙,但请查看根级字典中键"thread"处对象的打印值:

This is an NSDictionary with one key: "thread"
|
| This is an NSString object, see the quotes (")?
| |
V | Notice that these other keys/values have their quotes (") escaped with backslash?
{ V |
thread = "{ V
\"uuid\": \"e9290344dee38c42782\",
\"site_full\": \"www.independent.co.uk\",
\"main_image\":\"http://www.independent.co.uk/incoming/article10498560.ece/binary/original/39-tea-party-ap.jpg\"
}";
} ^
|
Here is the end of the NSString, again we see quotes (")

假设确实存在问题,则需要将字符串转换为实际的字典,您应该能够轻松地执行 using JSON serialization which this question/answer should help guide you to do.

解决此问题后,您对 valueForKeyPath:的使用应该正确并且应该立即开始工作。

关于ios - 获取NSDictionary值 objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32901094/

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