我试图从我的WordPress博客解析JSON提要。我有一个我需要使用的自定义字段,但无法使其与SBJson一起使用。这里是我的饲料看起来像(我已经剥离了其他无用的东西):iOS SBJson请求变差
{
"status":"ok",
"count":27,
"count_total":2552,
"pages":95,
"posts":[
{
"id":8978,
"type":"post",
"custom_fields":{
"btnNameScrollBlog":[
"<null>"
]
"author-name":[
"John Doe"
]
},
}
我想要得到作者的名字。 下面是我用于获取在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]];
}
我尝试设置custom_fields对象作为ID:ID定制= [交objectForKey:@ “custom_fields”];
,然后用它去作者的名字:[自定义objectForKey:@ “作者名”]
但我得到一个NSInvalidArgumentException '的,理由是:' - [__ NSArrayM rangeOfString:]:无法识别选择错误。
任何建议?
如果我尝试从帖子中获取类别标题,该怎么办?
"categories": [
{
"id": 360,
"slug": "deals",
"title": "Deals",
"description": "",
"parent": 0,
"post_count": 28
}
],
我会把类别放在这样的数组中吗?我如何从那里获得标题?我试过这个,并得到索引3的对象,但有一个错误。
NSArray *cat = [post objectForKey:@"categories"];
错误消息意味着“作者名”是一个NSArray但代码处理它,就好像它是一个NSString。 – Anna
您的Json格式不正确,请尝试使用http://jsonlint.com/ – Hakim