0

嗨,我正在使用下面的代码创建一个 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 

]
}

我会非常感谢你的帮助。

4

2 回答 2

4

你可以这样做:

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"50", @"resultCount", _list1, @"results", nil];

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                   options:0
                                                     error:&error];
于 2012-12-31T06:48:10.317 回答
1

在添加 _soccerList 行之前的 for 循环中,添加以下行

 NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:[self.items count],@"resultCount",nil];
 [self.list addObject:dic];
于 2012-12-31T07:06:41.043 回答