【问题标题】:SBJson Parsing without a keySBJson 无密钥解析
【发布时间】:2012-05-25 08:46:53
【问题描述】:

我通过使用单例客户端(RestKit)获得响应得到这个 json 字符串。

[
   {
      "created_at":"Tue Apr 24 19:25:14 +0000 2012",
      "id":132456,
   },
   {
      "created_at":"Tue Apr 24 19:25:14 +0000 2012",
      "id":13456,
   },
]

我得到了这个结果

NSDictionary *results = [[response bodyAsString] JSONValue];

结果是这样的

(
   {
      "created_at" = "Tue Apr 24 19:25:14 +0000 2012";
      "id"=132456;
   }
   {
      "created_at" = "Tue Apr 24 19:25:14 +0000 2012";
      "id"=132456;
   }
)

作为 NSDictionary。

如您所见,我没有可用的密钥。如果是这样,我会做这样的代码NSMutableArray *block = [results objectForKey:@"key"];。我试图为 objectForKey 放置和空字符串,但它不起作用并给了我一个错误“objectForKey”。

问题: 如何解析每个块以将其放入没有键的数组中?

请帮忙!谢谢!

【问题讨论】:

    标签: objective-c ios sbjson


    【解决方案1】:

    这实际上是一个包含 2 个字典的数组。更改您的代码:

    NSDictionary *results = [[response bodyAsString] JSONValue];
    

    NSArray *results = [[response bodyAsString] JSONValue];
    

    然后,如果需要,您可以使用 For-In 从该数组中获取所有字典。 Key 似乎是 id 属性。

    所以对于数组中的第一个:

    NSDictionary *dictionary = (NSDictionary *)[results objectAtIndex:0];
    NSInteger myKey = [[dictionary valueForKey@"id"] intValue];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多