2

我正在尝试从 JSON 响应中读取如下内容

{ 
"URL": 
  { 
     "target": "www.google.com", 
    "0": [ 92, 15 ], 
    "1": [ 92, 16 ], 
    "2": [ 74, 15 ], 
    "4": [ 5, 16 ] 
  } 
}

使用 SBJSON 我设法获得了“目标”字段,

testString = [[result objectForKey:@"URL"] objectForKey:@"target"];

当我执行 NSLOG 时,它显示 www.google.com

但这段代码不适用于其他密钥对。

testString = [[result objectForKey:@"URL"] objectForKey:@"0"];

当我尝试打印 testString 时,它给了我一个错误。

在控制台中,我打印了值,它们是,

(lldb) po testString
(NSString *) $4 = 0x0864b190 <__NSArrayM 0x864b190>(
65,
27
)

如何提取这些 65 和 27 ?

4

3 回答 3

1

做这个:

if([result objectForKey:@"URL"] objectForKey:@"0"] isKindofClass:[NSArray class])
{
   NSArray *arritems = [[result objectForKey:@"URL"] objectForKey:@"0"];
   NSMutableArray *values = [NSMutableArray array];
   for( id *item in arritems)
   {
      if([item is isKindOfClass:[NSString class]])
      {
         NSString * valueStr = item; 
         [values addObject:valueStr];
       }
      else if([item is isKindOfClass:[NSDecimalNumber class]])
      {
        NSDecimalNumber * valueNum = item; 
        [values addObject:valueNum];
       }
   } 
   NSLog(@"%@",values);
}

键值 1 、 2 、 3 的相同逻辑重复

于 2012-08-30T12:49:55.760 回答
1

那是一个对象数组。尝试:

NSArray * array = [[result objectForKey:@"URL"] objectForKey:@"0"];
id a = [array objectAtIndex:0]; // 65
id b = [array objectAtIndex:1]; // 27

// now determine the type of objects in the array, and use them appropriately:
if ([a isKindOfClass:[NSString class]]) {
  NSString * string = a;
  ...use appropriately
}
else if ([a isKindOfClass:[NSDecimalNumber class]]) {
  NSDecimalNumber * number = a;
  ...use appropriately
}
...
else {
  assert(0 && "type not supported");
}
于 2012-08-30T12:51:26.067 回答
0
NSArray *data = [[result objectForKey:@"URL"] objectForKey:@"0"];

然后你的物品在[data objectAtIndex:0]并且[data objectAtIndex:1]

于 2012-08-30T12:52:36.213 回答