2014-02-16 57 views
1

我試圖從我的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"]; 
+3

錯誤消息意味着「作者名」是一個NSArray但代碼處理它,就好像它是一個NSString。 – Anna

+0

您的Json格式不正確,請嘗試使用http://jsonlint.com/ – Hakim

回答

3

'-[__NSArrayM rangeOfString:]: unrecognized selector error.表示您將數組視爲字符串。您的代碼幾乎是正確的,你只需要得到從陣列(最好用檢查數組不爲空)的第一個項目,因爲

"author-name":[ 
    "John Doe" 
] 

是包含一個字符串數組。所以:

NSArray *names = [custom objectForKey:@"author-name"]; 
NSString *name = [names firstObject]; 

NSArray *categories = [custom objectForKey:@"categories"]; 
NSDictionary *category = [categories firstObject]; 
NSString *title = [category objectForKey:@"title"]; 
+0

您是對的,謝謝! –

+0

你可以幫助我與類別標題以及?我很困惑。 –

+0

它在哪裏嵌套?同樣,它包含一個數組,因此您需要索引到數組中以獲取字典並使用該鍵。 – Wain