【问题标题】:xcode counting JSON itemsxcode计数JSON项目
【发布时间】:2012-06-04 13:19:58
【问题描述】:

我需要在这个 JSON 响应中计算项目(帖子)的数量,

2012-06-04 14:09:57.872 horitable[72261:11903] JSON : {
posts =     (
            {
        post =             {
            eventdate = "2012-03-31";
            eventid = 2;
            eventimage = "http://hernandoz.local/~hernandoz/kopict/02_31march2012.jpg";
            eventinfo = "02 event";
            eventname = "xplosion 02";
        };
    },
            {
        post =             {
            eventdate = "2012-07-07";
            eventid = 3;
            eventimage = "http://hernandoz.local/~hernandoz/kopict/greg_vs_turner.jpg";
            eventinfo = "02 event";
            eventname = "Xplosion 02";
        };
    },
            {
        post =             {
            eventdate = "2012-04-29";
            eventid = 4;
            eventimage = "http://hernandoz.local/~hernandoz/kopict/ko_itclub_apr_2012.jpg";
            eventinfo = "KO East London Interclub";
            eventname = "KO Interclub";
        };
    }
);

}

我知道只有 3 个事件(帖子),这是我正在使用的代码

 [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
    NSLog(@"JSON : %@", JSON); //get the JSON response

    // 6.1 - Load JSON into internal variable
    jsonResponse = JSON;
    // 6.2 - Get the number of shows (post)
    int shows = 0;
    for (NSDictionary* day in jsonResponse) {
        shows += [[day objectForKey:@"posts"] count];

        NSLog(@"count : %d",shows);
    }

我得到一个错误,但我不明白为什么。

-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x76986f0

谁能帮帮我。谢谢

【问题讨论】:

  • 只是想确保你已经正确分配了 NSDictionary
  • jsonResponse 是 NSArray,JSON 是 ID

标签: iphone ios xcode json


【解决方案1】:

试试这个

NSLog(@"Response members= %@",responseString);
NSArray *array = [(NSDictionary*)[responseString JSONValue] objectForKey:@"posts"];
NSLog(@"Count value= %d",[array count]);    

【讨论】:

  • 您好,那 JSONValue 是什么?我没有使用任何外部库,只有 AFNetworking..谢谢
【解决方案2】:

在你的情况下,你可以这样做

 [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
 NSLog(@"JSON : %@", JSON); //get the JSON response

// 6.1 - Load JSON into internal variable
jsonResponse = JSON;
// 6.2 - Get the number of shows (post)
int shows = [[jsonResponse objectForKey:@"posts"] count];

   NSLog(@"count : %d",shows);

【讨论】:

  • 我收到一个错误 - NSarray 没有可见的@interface 声明选择器'objectforkey'
  • objectForKey 不是 objectforkey
  • 是的,这是我输入错误的方式,但它是 Xcode 中的 objectForKey
  • 如果你做 NSLog(@"jsonResponse : %@", jsonResponse);
  • 我在 JSON 响应中得到相同的结果
【解决方案3】:

你需要先将json分片为

NSDictionary * dict = [JSON JSONValue];

NSDictionary * dict = [JSON JSONFragmentValue];

然后

for (NSDictionary* day in dict) {
    shows += [[day objectForKey:@"posts"] count];

    NSLog(@"count : %d",shows);
}

【讨论】:

  • 是JSon库的函数,将json数据转换成字典或数组。
  • 下载 sbjson 并使用它。
【解决方案4】:

问题是 JSON 作为 ID,这可行

jsonResponse = [(NSDictionary*)JSON objectForKey:@"posts"];

【讨论】:

    【解决方案5】:

    我认为您错误地将“帖子”而不是“帖子”。键“posts”包含一个字典数组,每个字典都有一个键“post”。你正在做的是你从数组中取出所有字典行

      for (NSDictionary* day in jsonResponse) {
    

    并检查字典中的关键“帖子”。确实,字典中没有称为“帖子”的键。我是“帖子”。 “post”的值是 NSDictionary 而不是数组。所以你不能在那里打电话给count。您的问题的解决方案是删除 for 循环中不必要的 API

      [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
      jsonResponse = JSON;
    
      int shows = 0;
      for (NSDictionary* day in jsonResponse) {
          shows += 1;
      }
    
      NSLog(@"count : %d",shows);
    

    【讨论】:

      猜你喜欢
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多