嗨,我正在使用下面的代码创建一个JSON请求,并且它的工作正常:
for (int i=0; i<= [urls count]-1; i++) {
self.items=[[NSMutableDictionary alloc] init];
[self.items setObject:[names objectAtIndex:i] forKey:@"results"];
[self.items setObject:[urls objectAtIndex:i] forKey:@"URL"];
[self.list addObject:_soccerList];
}
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:_list
options:0
error:&error];
self.results=[NSJSONSerialization
JSONObjectWithData:jsonData
options:0
error:&error];
但是,我想将此标题添加到JSON请求中,我在其中添加项目的数量。你们中的任何一个知道我该怎么做?
{
"resultCount":50,
"results": [
/// json content
]
}
我真的很感谢你的帮助。
答案 0 :(得分:4)
你可以这样做:
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"50", @"resultCount", _list1, @"results", nil];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
options:0
error:&error];
答案 1 :(得分:1)
在添加_soccerList的行之前的for循环中,添加以下行
NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:[self.items count],@"resultCount",nil];
[self.list addObject:dic];