我試圖從我的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