Ios 如何获取json数据并将数组整数值的字典打印到我的tableview单元格中
Haii我有json数据,比如Ios 如何获取json数据并将数组整数值的字典打印到我的tableview单元格中,ios,objective-c,json,uitableview,Ios,Objective C,Json,Uitableview,Haii我有json数据,比如 { NetworkMembers = ( { id = 1; memberId = 1; networkId = 1; position = 0; type = H; }, 最后,我将其放入如下数组: for (int i=0; i<[array count]; i++) { NSDictionary *dict
{
NetworkMembers = (
{
id = 1;
memberId = 1;
networkId = 1;
position = 0;
type = H;
},
最后,我将其放入如下数组:
for (int i=0; i<[array count]; i++) {
NSDictionary *dictionary = [array objectAtIndex:i];
NSArray *array2=[dictionary valueForKey:@"NetworkMembers"];
for (int j=0; j<[array2 count]; j++) {
NSDictionary *dictionary2 = [array2 objectAtIndex:j];
NSLog(@"J value %@",[dictionary2 valueForKey:@"type"]);
[self.tablArray addObject:[dictionary2valueForKey:@"type"]];
[self.tablArray addObject:[dictionary2 valueForKey:@"id"]];
}
}
tableArray (
{
id = 1;
type = H;
},
{
id = 2;
type = H;
}
)
cell.textLabel.text = [[tablArray objectAtIndex:indexPath.row] objectForKey:@"type"];
if([tablArray count]>0)
{
cell.detailTextLabel.text=[[tablArray objectAtIndex:indexPath.row] objectForKey:@"id"];
}
但打印详细文本标签时,我遇到了如下错误
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[\uu NSCFString stringValue]:未识别的选择器已发送到实例0x109314510” *第一次抛出调用堆栈:
提前感谢有人能帮我吗我想最好是得到一个字典数组,其中包含一个类型键和一个id键,因此
表数组
将是:
// Create a property of tableArray to retrieve data later
@property (nonatomic, strong) NSMutableArray *tableArray
// Add data inside your tableArray your viewDidLoad
NSDictionary *dicAllNetworks = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; // Retrieve data from your JSON file inside a NSDictionary
_tableArray = [NSMutableArray array];
for (NSDictionary *dic in [dicAllNetworks objectForKey:@"NetworkMembers"])
{
[tableArray addObject:@{@"id" : [dic objectForKey:@"id"], @"type": [dic objectForKey:@"type"]}];
}
NSLog(@"%@", tableArray);
您将得到如下阵列:
for (int i=0; i<[array count]; i++) {
NSDictionary *dictionary = [array objectAtIndex:i];
NSArray *array2=[dictionary valueForKey:@"NetworkMembers"];
for (int j=0; j<[array2 count]; j++) {
NSDictionary *dictionary2 = [array2 objectAtIndex:j];
NSLog(@"J value %@",[dictionary2 valueForKey:@"type"]);
[self.tablArray addObject:[dictionary2valueForKey:@"type"]];
[self.tablArray addObject:[dictionary2 valueForKey:@"id"]];
}
}
tableArray (
{
id = 1;
type = H;
},
{
id = 2;
type = H;
}
)
cell.textLabel.text = [[tablArray objectAtIndex:indexPath.row] objectForKey:@"type"];
if([tablArray count]>0)
{
cell.detailTextLabel.text=[[tablArray objectAtIndex:indexPath.row] objectForKey:@"id"];
}
接下来,按如下方式修改代码:
for (int i=0; i<[array count]; i++) {
NSDictionary *dictionary = [array objectAtIndex:i];
NSArray *array2=[dictionary valueForKey:@"NetworkMembers"];
for (int j=0; j<[array2 count]; j++) {
NSDictionary *dictionary2 = [array2 objectAtIndex:j];
NSLog(@"J value %@",[dictionary2 valueForKey:@"type"]);
[self.tablArray addObject:[dictionary2valueForKey:@"type"]];
[self.tablArray addObject:[dictionary2 valueForKey:@"id"]];
}
}
tableArray (
{
id = 1;
type = H;
},
{
id = 2;
type = H;
}
)
cell.textLabel.text = [[tablArray objectAtIndex:indexPath.row] objectForKey:@"type"];
if([tablArray count]>0)
{
cell.detailTextLabel.text=[[tablArray objectAtIndex:indexPath.row] objectForKey:@"id"];
}
我把ony type和id放在数组中,我在表单元格中获取type,但有一个问题是详细文本标签您的数组不包含nsstring,而我认为它包含NetworkMembers。因此,您必须在获取或插入数组时进行类型转换。首先,在控制台中打印
tableArray
它是什么样子的?在收到JSON时添加代码,我将向您展示如何获取dictionarytableArray数组的数组(H,1,H,2,H,3,H,4,H,6)我只接受类型和田园诗,但在这里,您无法正确使用此数组,因为您不知道该类型的id。相信我,由于未捕获的异常“NSInvalidArgumentException”,dictionary将更适合Youterming应用程序,原因:'-[\uu NSCFString objectForKey:]:未识别的选择器发送到实例0x109542850'***第一次抛出调用堆栈:它将崩溃我收到相同的消息