如何编码json响应并将其写入数组?

时间:2013-09-07 16:31:50

标签: ios json nsarray afnetworking afjsonrequestoperation

我正在使用AFJSONRequestOperation来获取我朋友的列表。这是请求:

/// get friends
    NSString *str = [NSString stringWithFormat:@"https://api.vk.com/method/friends.get?fields=first_name,last_name&uid=%@&access_token=%@", user_id, secret];
    NSURL *url = [[NSURL alloc] initWithString:str];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:JSON
                                                           options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                             error:&error];

                    if (! jsonData) {
                        self.friendsList.hidden = YES;
                        NSLog(@"Got an error: %@", error);
                    } else {
                        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
                        NSLog(@"JSON: %@", jsonString);
                    }


    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];

NSLog的输出如下所示:

"response" : [
{
  "user_id" : 3468700,
  "uid" : 3468700,
  "first_name" : "Юленька",
  "last_name" : "Калюжная",
  "online" : 0
},
{
  "user_id" : 3870082,
  "uid" : 3870082,
  "first_name" : "Михаил",
  "last_name" : "Халецкий",
  "online" : 0
},
{
  "user_id" : 4080134,
  "uid" : 4080134,
  "first_name" : "Мария",
  "last_name" : "Караваева",
  "online" : 0
},
{
  "user_id" : 4943555,
  "uid" : 4943555,
  "first_name" : "Юсёнок",
  "last_name" : "Ларченко",
  "online" : 0
},
{
  "user_id" : 5012692,
  "uid" : 5012692,
  "first_name" : "Вячеслав",
  "last_name" : "Тарабушкин",
  "online" : 0
},
{
  "user_id" : 5036666,
  "uid" : 5036666,
  "first_name" : "Владислав",
  "last_name" : "Растегаев",
  "online" : 0
}

问题是json响应是一条线,我不能解决大量的元素。有没有机会将json的反应转化为大规模?我试过替换

 NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; for 

 NSArray *ar = [[NSArray alloc] initWithObjects:jsonData, nil];

结果NSLog显示以下内容:

 (<7b0a2020 22726573 706f6e73 6522203a 205b0a20 2020207b 0a202020 20202022 75736572          5f696422 203a2033 34363837 30302c0a 20202020 20202275 69642220 3a203334 36383730 302c0a20 20202020 20226669 7273745f 6e616d65 22203a20 22d0aed0 bbd0b5d0 bdd18cd0 bad0b022 2c0a2020 20202020 226c6173 745f6e61 6d652220 3a2022d0 9ad0b0d0 bbd18ed0 b6d0bdd0 b0d18f22 2c0a2020 20202020 226f6e6c 696e6522 203a2030 0a202020 207d2c0a 20202020 7b0a2020 20202020 22757365 725f6964 22203a20 33383730 3038322c 0a202020 20202022 75696422 203a2033 38373030 38322c0a>)

我也试过这个变种:

  NSArray *dataDictionary = [NSJSONSerialization JSONObjectWithData:JSON options:0 error:&error];
  NSLog(@"%@", dataDictionary); 

但应用程序倾向于关闭,告知有错误: [__NSCFDictionary bytes]:无法识别的选择器发送到实例0x7527ad0 任何帮助,将不胜感激。提前致谢

2 个答案:

答案 0 :(得分:0)

当您使用AFJSONRequestOperation时,框架会为您完成JSON的解析,您只需要知道/检查结果是NSArray还是{{1}然后根据需要访问它。

答案 1 :(得分:0)

您需要检查JSON对象是NSArray类型的对象还是NSDictionary类型的对象,如下所示:

if([JSON isKindOfClass:[NSDictionary class]]) {
    // JSON object is NSDictionary type of object.
} else if([JSON isKindOfClass:[NSArray class]]) {
   // JSON object is NSArray type of object.
}