Iphone Can';我无法从JSON中获取所有数据
我正在将一个JSON解析到我的MASTER-DETAIL应用程序中,在“深入”JSON时遇到了问题。我无法在我的Iphone Can';我无法从JSON中获取所有数据,iphone,json,tableview,master-detail,Iphone,Json,Tableview,Master Detail,我正在将一个JSON解析到我的MASTER-DETAIL应用程序中,在“深入”JSON时遇到了问题。我无法在我的detailTableView中获取数据。 在我的detailTableView中,我想知道这里的hotels/pousadas的名称 请参阅my JSON和detailTableView.m: [ { "title": "Where to stay", "pousadas": [ { "beach"
detailTableView
中获取数据。
在我的detailTableView
中,我想知道这里的hotels/pousadas的名称
请参阅my 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": "hotelarrastao_reserva@hotmail.com",
"image": "test.jpg",
"latitude": "-23.753355",
"longitude": "-45.401946"
}
]
}
]
- (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"];
以及detailTableView中的tableView。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": "hotelarrastao_reserva@hotmail.com",
"image": "test.jpg",
"latitude": "-23.753355",
"longitude": "-45.401946"
}
]
}
]
- (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;
}
提前谢谢 您读取的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": "hotelarrastao_reserva@hotmail.com",
"image": "test.jpg",
"latitude": "-23.753355",
"longitude": "-45.401946"
}
]
}
]
现在,您可以在这里访问各种值,例如“pousadas”数组
现在,就像我们使用初始字典一样,我们可以访问pousadas数组中的第一个对象
NSArray *pousadas = initialDictionary[@"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数据):
您有一个由一个元素组成的数组,其中包含一个字典和两个元素--“title”和“pousadas”。“pousadas”又是一个包含一个元素的数组。其中一个元素是包含其余数据的字典。洋葱去皮,一层一层。(NSLog是你的朋友——它将提供一个与原始JSON非常相似的中间数据视图,让你很容易看到下一步需要做什么。)Saved my day@Aaron Wojnowski。也很有教育性!