Reputation: 655
I'm trying to parse a JSON feed from my wordpress blog. I've got a custom field I need to use but can't get it to work with SBJson. Here's how my feed looks like (I've stripped the other useless stuff):
{
"status":"ok",
"count":27,
"count_total":2552,
"pages":95,
"posts":[
{
"id":8978,
"type":"post",
"custom_fields":{
"btnNameScrollBlog":[
"<null>"
]
"author-name":[
"John Doe"
]
},
}
I'm trying to get the author's name. Here's the code I used for getting the feed on iOS:
-(void)downloadRecentPostJson{
[recentPost removeAllObjects];
[previous_total_per_page removeAllObjects];
NSURL *urls = [NSURL URLWithString:[NSString stringWithFormat:@"%@/?json=%@",url,methodJson]];
NSData *sa = [NSData dataWithContentsOfURL:urls];
NSString *jsonString = [[NSString alloc] initWithData:sa encoding:NSUTF8StringEncoding];
NSDictionary *result = [jsonString JSONValue];
NSArray* posts = [result objectForKey:@"posts"];
total_page = [[result objectForKey:@"pages"] intValue];
total_post_per_page = [[result objectForKey:@"count"] intValue];
[previous_total_per_page addObject:[result objectForKey:@"count"]];
current_page = 1;
for (NSDictionary *post in posts) {
id custom = [post objectForKey:@"custom_fields"];
id thumbnail = [post objectForKey:@"thumbnail"];
NSString *featuredImage = @"";
if (thumbnail != [NSNull null])
{
featuredImage = (NSString *)thumbnail;
}
else
{
featuredImage = @"0";
}
[recentPost addObject:[NSArray arrayWithObjects:[post objectForKey:@"id"],[post objectForKey:@"title_plain"],[post objectForKey:@"excerpt"],featuredImage,[post objectForKey:@"content"],[post objectForKey:@"date"],[post objectForKey:@"comments"],[post objectForKey:@"comment_status"],[post objectForKey:@"scrollBlogTemplate"],[post objectForKey:@"url"],[post objectForKey:@"specialBtn"],[post objectForKey:@"btnNameScrollBlog"],[post objectForKey:@"latScrollBlog"],[post objectForKey:@"longScrollBlog"],[post objectForKey:@"openWebUrlScrollBlog"],[post objectForKey:@"gallery"], [custom objectForKey:@"author-name"], nil]];
}
I tried setting the custom_fields object as an id: id custom = [post objectForKey:@"custom_fields"];
And then using it to get to the author's name: [custom objectForKey:@"author-name"]
But I get an NSInvalidArgumentException', reason: '-[__NSArrayM rangeOfString:]: unrecognized selector error.
Any suggestions??
What if I try and get the category title from the post?
"categories": [
{
"id": 360,
"slug": "deals",
"title": "Deals",
"description": "",
"parent": 0,
"post_count": 28
}
],
Do I put the categories in an array like this? How do I get the title from that? I tried this and getting the object at index 3, but got an error.
NSArray *cat = [post objectForKey:@"categories"];
Upvotes: 1
Views: 190
Reputation: 119031
'-[__NSArrayM rangeOfString:]: unrecognized selector error.
means that you are treating an array as a string. Your code is nearly correct, you just need to get the first item from the array (preferably with a check that the array isn't empty) because
"author-name":[
"John Doe"
]
is an array containing one string. So:
NSArray *names = [custom objectForKey:@"author-name"];
NSString *name = [names firstObject];
NSArray *categories = [custom objectForKey:@"categories"];
NSDictionary *category = [categories firstObject];
NSString *title = [category objectForKey:@"title"];
Upvotes: 3