我的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];
}
}
答案 0 :(得分:4)
看起来存储在索引下标"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:
的使用应该是正确的,并应立即开始工作。
答案 1 :(得分:1)
试试这个现代目标-C代码:
NSDictionary *myDict = @{@"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"}};
NSLog(@"Image = %@", myDict[@"thread"][@"main_image"]);
编辑:在OP响应附加调试器屏幕截图后: