使用NSURLSession破坏结果的JSON读取

时间:2016-02-29 16:08:36

标签: ios objective-c json nsurlsession nsjsonserialization

如上所述,我正在从URL读取JSON文件。如果我访问页面,JSON格式是有效和好的。在应用程序中,我得到它:

NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:urlAsString]
        completionHandler:^(NSData *data,
                            NSURLResponse *response,
                            NSError *error) {
            if (error) {
                NSLog(@"Error");
            } else {
                alreadyParsed = true;

                NSMutableDictionary* object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
                NSMutableDictionary *posts = [object valueForKey:@"posts"];

                if (!object) {
                    NSLog(@"Error parsing JSON: %@", error);
                } else {
                    for (NSMutableDictionary *item in posts) {
                        NSLog(@"Item: %@", item);
                    }
                }

                NSLog(@"Data: %@", [object description]);
            }
        }] resume];

在页面上,JSON看起来像:

    {
      "status": "ok",
      "count": 1,
      "count_total": 39,
      "pages": 39,
      "posts": [
        {
          "id": 948564,
          "title": "",
          "modified": "2016-02-17 21:32:26",
          "attachments": [],
"attachments_featured": [
        {}]
        },
        {
          "id": 948565,
          "title": "",
          "modified": "2016-02-17 21:32:26",
          "attachments": []
        }]
    }

但是在app中,我得到了:

2016-02-29 17:04:34.819 test[22835:1929421] Data: {
    count = 1;
    "count_total" = 39;
    pages = 39;
    posts =     (
                {
            attachments =             (
            );
.
.
.

在帖子数组和附件之间应该有更多的数据,但我无法弄清楚发生了什么以及为什么它没有显示。

我尝试将页面数据转换为字符串并打印它并显示正常。还尝试将字符串转换为NSData并创建JSON,但结果是相同的。任何人都知道发生了什么事?

0 个答案:

没有答案