我使用以下代码将JSON数据(从SOAP服务)转换为NSDictionary
。
-(void)retriveFromSYSoapTool:(NSMutableArray *)_data{
NSLog(@"data: %@",_data);
NSArray *value = [_data valueForKey:@"GetDemoDataResult"];
NSError *error;
NSData *objData = [value[0] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"%@",json);
}
输出
2017-04-04 13:03:51.085 SoapService[18444:588594] (
{
firstName = "1 Jhon";
lastName = Macay;
},
{
firstName = "2 William";
lastName = Proter;
},
{
firstName = "3 Joe";
lastName = Root;
},
{
firstName = "4 Brendon";
lastName = Haejoy;
},
{
firstName = "5 Jhon";
lastName = Snow;
},
{
firstName = "6 Theon";
lastName = Greyjoy;
}
)
我是否需要将此转换为其他?或者如何在UITable View中绑定上述输出?
答案 0 :(得分:2)
要使用表格视图,您需要数组
Checkout this简单表格视图教程
答案 1 :(得分:0)
选择一个NSMutuableArray
并将dictionary
添加到此array
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject: json];
//Than Reload Tableview
Note: Declare array Globally to access in your class
tableview
显示
cell.label.text = [[array objectAtIndex:indexPath.row]valueForKey:@"firstName"];
答案 2 :(得分:0)
将 json 数据存储到全球声明NSArray 。
-(void)retriveFromSYSoapTool:(NSMutableArray *)_data{
NSLog(@"data: %@",_data);
NSArray *value = [_data valueForKey:@"GetDemoDataResult"];
NSError *error;
NSData *objData = [value[0] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"%@",json);
DataArray = [NSArray arrayWithObjects:json, nil];
[tableView reloadData];
}
此处 DataArray 是全局声明的NSArray对象;
现在编写UITableView数据源方法。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return DataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"jsoncell" forIndexPath:indexPath];
NSDictionary *dict = DataArray[indexPath.row];
NSString *output = [NSString stringWithFormat:@"%@ %@",dict[@"firstName"],dict[@"lastName"]];
cell.textLabel.text = output;
return cell;
}
答案 3 :(得分:0)
应该是这样的
在.h文件中声明jsonArray
@property (strong, nonatomic) NSMutableArray *jsonArray;
添加以下行viewDidLoad
self.jsonArray = [[NSMutableArray alloc]init];
-(void)retriveFromSYSoapTool:(NSMutableArray *)_data{
NSLog(@"data: %@",_data);
NSArray *value = [_data valueForKey:@"GetDemoDataResult"];
NSError *error;
NSData *objData = [value[0] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"%@",son);
[self.jsonArray addObject:[[json objectForKey:@"firstname"]stringByAppendingString:[json objectForKey:@"lastname"]];
[tableViewName reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.jsonArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = [self.jsonArray objectAtIndex:indexPath.row];
return cell;
}