无法从我的JSON中获取所有数据

无法从我的JSON中获取所有数据

问题描述:

我正在解析JSON到我的MASTER-DETAIL应用程序,并在将“深入挖掘”到JSON中时出现问题。我无法获取我的detailTableView中的数据。 在我的detailTableView中,我想要命名酒店/ pousadas。无法从我的JSON中获取所有数据

见我的JSON和detailTableView.m:

[ 

    { 
     "title": "Where to stay", 
     "pousadas": 
    [ 
     { 
      "beach": "Arrastão", 
      "name": "Hotel Arrastão", 
      "address": "Avenida Dr. Manoel Hipólito Rego 2097", 
      "phone": "+55(12)3862-0099", 
      "Email": "[email protected]", 
      "image": "test.jpg", 
      "latitude": "-23.753355", 
      "longitude": "-45.401946" 
     } 
    ] 
    } 

] 

而且在的tableView detailTableView.m:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return self.stayGuide.count; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailCellStay"]; 

这里是我的选拔赛:

NSString *pous = [self.stayGuide valueForKey:@"name"]; 

    NSLog([self.stayGuide valueForKey:@"name"]); 

    cell.textLabel.text = pous; 



    return cell; 
} 

提前感谢!

+0

您有一个包含字典有两个元素,一个元素的数组 - “标题”和“Pousadas酒店”。 “pousadas”又是一个包含一个元素的数组。这一个元素是一个包含其余数据的字典。剥洋葱,一次一层。 (NSLog是你的朋友 - 它将呈现与原始JSON非常相似的中间数据视图,从而可以轻松查看每个步骤下一步需要执行的操作。) – 2013-02-21 23:10:23

您正在阅读的JSON不正确!让我们来看看你的数据:

[ <---- array 
    { <---- dictionary 
     "title": "Where to stay", 
     "pousadas": [ <---- array 
      { <---- dictionary 
       "beach": "Arrastão", 
       "name": "Hotel Arrastão", 
       "address": "Avenida Dr. Manoel Hipólito Rego 2097", 
       "phone": "+55(12)3862-0099", 
       "Email": "[email protected]", 
       "image": "test.jpg", 
       "latitude": "-23.753355", 
       "longitude": "-45.401946" 
      } 
     ] 
    } 
] 

假设你有存储在“stayGuide”属性(这应该是类型的NSArray的)的数据,你可以访问初始字典,像这样:

NSDictionary *initialDictionary = [self stayGuide][0]; // access using new Objective-C literals 

现在,您可以访问这里的各种值,例如“pousadas”数组。

NSArray *pousadas = initialDictionary[@"pousadas"]; 

现在,就像我们对初始字典所做的那样,我们可以访问pousadas数组中的第一个对象。

NSDictionary *dictionary = pousadas[0]; 

最后,我们可以在第一个pousadas字典中访问这些键的一部分。

NSString *beach = dictionary[@"beach"]; 
NSString *name = dictionary[@"name"]; 
NSString *address = dictionary[@"address"]; 

NSLog(@"Beach: %@, Name: %@, Address: %@"beach,name,address); 

在未来,你可能会希望stayGuide属性等于pousadas数组。您可以设置它像这样(其中initialJSONArray是你的出发JSON数据):

[self setStayGuide:initialJSONArray[0][@"pousadas"]]; 
+0

保存我的一天@Aaron Wojnowski。非常教学也! – 2013-02-26 00:10:39