ios - iOS SBJson请求失败

标签 ios objective-c json sbjson

我正在尝试从我的wordpress博客中解析JSON提要。我有一个需要使用的自定义字段,但无法与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 custom = [post objectForKey:@“custom_fields”];

然后使用它来获得作者的姓名:[custom objectForKey:@“author-name”]

但是我得到一个NSInvalidArgumentException',原因:'-[__ NSArrayM rangeOfString:]:无法识别的选择器错误。

有什么建议么??

如果我尝试从帖子中获取类别标题怎么办?
  "categories": [
                {
                    "id": 360,
                    "slug": "deals",
                    "title": "Deals",
                    "description": "",
                    "parent": 0,
                    "post_count": 28
                }
            ],

我是否将类别放在这样的数组中?我如何从中获得标题?我尝试了此操作,并在索引3处获取了对象,但出现了错误。
   NSArray *cat = [post objectForKey:@"categories"];

最佳答案

'-[__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"];

关于ios - iOS SBJson请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21814527/

相关文章:

c# - 添加 json :Array attribute to an xml node before serializing 时出现意外结果

ios - Cordova Plugin.xml 将未定义的键写入 info.plist

iphone - 应用程序在后台时获取屏幕截图? (允许私有(private) API)

ios - 如何滑动以编辑 tableView 中的文本?

ios - 约束变化不起作用

ios - UITableView 设置背景颜色

json - JSON 对象内元素的 SwiftyJSON 顺序

php - AJAX/PHP - 通过一次 ajax 调用填充不同类型的输入字段

ios - 如何使来自 UIWebView 的音频静音

iphone - 无法将两个图像合并为一个