我必须在以下json中显示TABLEVIEW中的所有全名值。我怎样才能做到这一点?下面是我的json数据的样子。我应该将所有转换为数组还是我可以使用字典?
"Beneficiaries": [
{
"BeneficiaryType": "Individual",
"CustomerID": 512,
"ID": 258,
"Title": "Mr",
"Firstname": "zympay",
"Surname": "test1",
"Fullname": "zympay test1",
"Address1": "East Central",
"Address2": "EC",
"City": "EC",
"State": "Londan",
"Country": "ZW",
"Telephone": "26322334455",
"Mobile": "4443523452345",
"Email": "zympaytest1@mailinator.com",
"isDefault": true,
"DateOfBirth": "/Date(510969600000-0000)/",
"PreferredService": "Cash Payment",
"Account": {},
"Bills": [
{
"ID": 38,
"ProviderID": 4,
"ProviderName": "Test Provider",
"PaymentReference": "Test Provider"
},
{
"ID": 39,
"ProviderID": 4,
"ProviderName": "Test Provider",
"PaymentReference": "Test Provider"
}
]
},
{
"BeneficiaryType": "Individual",
"CustomerID": 512,
"ID": 283,
"Title": "Mr",
"Firstname": "kandan",
"Surname": "Mani",
"Fullname": "kandan Mani",
"Address1": "19 Street",
"Address2": "Chennai",
"City": "Chennai",
"Zipcode": "",
"State": "TN",
"Country": "ZW",
"Mobile": "2632345234523",
"Email": "mani@gmail.com",
"CardNumber": "",
"isDefault": false,
"DateOfBirth": "/Date(157185960000-0000)/",
"PreferredService": "Cash Payment",
"Account": {
"AccountNumber": "",
"AccountName": "",
"BankAddress": "",
"IBAN": "",
"SWIFT": "",
"BankName": "",
"BankCity": ""
},
我使用以下代码将json转换为Dictionary。
NSData *GetBeneficiaries = [newStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *parsejson = [NSJSONSerialization JSONObjectWithData:GetBeneficiaries options:0 error:nil];
答案 0 :(得分:2)
您可以使用数组来存储“受益人”。让我们按照以下说明进行操作: -
NSMutableArray * beneficiariesArr = [[NSMutableArray alloc] initWithArray:[parsejson objectForKey:@"Beneficiaries"]];
然后
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
cell.textLabel.text = [[beneficiariesArr objectAtIndex:indexPath.row] objectForKey:@"Fullname"];
#pragma -Mark UITableView Delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return beneficiariesArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44.0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellID = @"CellID";
UITableViewCell * cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellID];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [[beneficiariesArr objectAtIndex:indexPath.row] objectForKey:@"Fullname"];
return cell;
}
答案 1 :(得分:1)
您可以将其保留原样,但最好将其转换为自定义对象数组,以便您可以使用应用中其他位置的数据执行更多操作。
例如,您可以提供通过网络将数据序列化到数据库或在数据集上执行更复杂计算的方法。
当数据在自定义对象中而不是使用字符串来表示所有内容的通用字典(更不用说效率)时,这变得更加容易。