我有以下类型的json响应:
class Player:
def getchips(self):
return self.chips
def setchips(self, add):
self.chips = self.chips + add
class result():
if totalplayer > opponenttotal:
print "You have " + str(totalplayer)
print self.opponent.getname() + " has " + str(opponenttotal)
print "You win!"
self.humanplayer.setchips(bet*2)
self.humanplayer.setscore()
self.replay()
class Game:
def __init__(self):
self.deck = Deck()
self.humanplayer = Player()
依旧......
我尝试了以下内容 -
在数组中存储响应对象(responseArray)。首先实现for循环从0到responseArray.count,将post的值存储在数组中(postArray)。在第一个循环中,我实现了从0到postArray.count的第二个循环,并返回了postarray.count的count值。
编辑 - 添加了循环
[{
cat_name : value
post : [{
title:value
image:value
content:value
},
{
title:value
image:value
content:value
},
{
title:value
image:value
content:value
}]
},{
cat_name : value
post : [{
title:value
image:value
content:value
},
{
title:value
image:value
content:value
}]
},
{
cat_name : value
post : [{
title:value
image:value
content:value
}]
}]
它发出警告,第二个循环不会增加,只会运行一次。
如何设置numberOfRowsInSection的返回值?
答案 0 :(得分:0)
从第二个for循环中删除行
return postArrayList.count;
如果你的方法想要返回一些东西,那么在for循环之外添加return语句。
答案 1 :(得分:0)
@interface TableViewController ()
@property(nonatomic)NSArray *responseArrayList;
@end
转换为JSON对象并将其存储在全局变量
中- (void)viewDidLoad {
[super viewDidLoad];
myJsonString = //here your JSON string response;
self.responseArrayList = [NSJSONSerialization JSONObjectWithData:[myJsonString dataUsingEncoding:NSUTF8StringEncoding]options:0 error:NULL];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.responseArrayList.count;
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section{
NSArray *postArrayList = [[responseArrayList objectAtIndex:section] valueForKey:@"post"];
return postArrayList.count;
}
答案 2 :(得分:0)
我建议写这样的代码......
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
self.myarray=dict[@"result”][@“post”];
//store the value in newObject
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@ my object is ",obj);
myobject *object = [[myobject alloc]init];
object.name = obj.productName;
object.image = obj.productimage;
[_myarray addObject:object];
}];