2013-01-23 38 views
0

我是JSON和Web服务的新手,因此非常适合我。我收到了一个来自Web服务器的响应。在邮递员,它看起来是这样的:设置为对象时JSON结果为空,但结果可以打印出来

{ 
    "Response": { 
     "TranscriptSource": "MD", 
     "Result": [ 
      { 
       "Variant": { 
        "Chromosome": "chr1", 
        "Position": 13302, 
        "ReferenceAllele": "C", 
        "VariantAlleles": "T" 
       } 
      } 
     ], 
     "JobId": 0 
    } 
} 

如果我connectionDidFinishLoading方法,我这样做:

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.receivedData options:kNilOptions error:&error]; 
[dict enumerateKeysAndObjectsUsingBlock:^(id obj, id key, BOOL *stop) { 
    NSLog(@"key: %@ obj: %@\n", [key description], [obj description]); 
}]; 

我得到的结果相同。基本上我想要那个“变体”字段。所以我想我会先做

id result = [dict [email protected]"Result"]; 

当我通过调试器,结果是零。我不知道为什么,因为我可以打印出来。

最后,我的主要问题是,如何访问响应的变体部分,但如果您知道为什么id result将是零,那也会很酷。谢谢!

回答

3

您的顶层项目是带有单个键“Response”的对象。它看起来像你想要的:

id result = [[dict objectForKey:@"Response"] objectForKey:@"Result"]; 
+1

而要到'Variant'字段,你需要挖掘到包含它的数组,所以要花费上述,第一个'Variant'条目将在'[[ [[dict objectForKey:@“Response”] objectForKey:@“Result”] objectAtIndex:0] objectForKey:@“Variant”]; – Mathew