【问题标题】:NSJSON "date" returns nullNSJSON“日期”返回 null
【发布时间】:2012-08-15 23:22:05
【问题描述】:

当我尝试自己的数据时,“日期”的 NSLog 返回 null。这是来自网页的我的 json:

[{"date":"2012-08-13 12:00:00","timezone_type":3,"timezone":"EST5EDT"},{"date":"2012-08-13 10: 00:00","timezone_type":3,"timezone":"EST5EDT"},{"date":"2012-08-13 13:00:00","timezone_type":3,"timezone":"EST5EDT "}]

这是我的代码:.m

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
//#define kLatestKivaLoansURL [NSURL          URLWithString://@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]
#define kLatestKivaLoansURL [NSURL URLWithString:@"http://www.xxxxxxxxxxx.com/appointjson.php"]

#import "MyAvailTimesViewController.h"

@interface MyAvailTimesViewController ()

@end

@implementation MyAvailTimesViewController
@synthesize json;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //Do any additional setup after loading the view.
    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: 
                        kLatestKivaLoansURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) 
                               withObject:data waitUntilDone:YES];

    });
}
- (void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSLog(@"%@",responseData);
    NSError* error;
    NSDictionary* json = [NSJSONSerialization 
                          JSONObjectWithData:responseData

                          options:kNilOptions 
                          error:&error];

    NSArray* latestLoans = [json objectForKey:@"date"];
    NSLog(@"date: %@", latestLoans);
    //NSLog(@"output2: %@", json);

.h

#import <UIKit/UIKit.h>

@interface MyAvailTimesViewController : UIViewController {
}

@property(nonatomic, retain) NSDictionary *json;
@end

我 responseData 时的 NS 日志是: 2012-08-15 15:17:54.114 GBSB [480:15b03]

我很难找到解决办法。

【问题讨论】:

  • 嗯...我不认为json 应该是NSDictionary。它看起来更像NSArray。您可以通过json.count 验证它是字典还是数组。
  • Tommy 下面阐述的正是我的意思。
  • 唯一的问题是我正在从服务器拉取数据。
  • 为什么会有问题?你正在做NSDictionary* json = [NSJSONSerialization...。你应该改为NSArray* json = [NSJSONSerialization...。如果你现在做的没有问题,那么你应该做的应该没有问题。
  • 我现在明白了 2012-08-15 18:02:04.406 GBSB[4019:15b03] 解析 JSON 时出错:错误 Domain=NSCocoaErrorDomain Code=3840 “操作无法完成。(Cocoa错误 3840。)”(JSON 文本没有以数组或对象开头,并且允许未设置片段的选项。) UserInfo=0x844cb40 {NSDebugDescription=JSON 文本没有以数组或对象开头,并且允许未设置片段的选项。}跨度>

标签: objective-c xcode ios5 json nsjsonserialization


【解决方案1】:

这个:

[{"date":"2012-08-13 12:00:00","timezone_type":3,"timezone":"EST5EDT"},{"date":"2012-08-13 10:00:00","timezone_type":3,"timezone":"EST5EDT"},{"date":"2012-08-13 13:00:00","timezone_type":3,"timezone":"EST5EDT"}]

是一个数组,而不是字典。当您执行[NSJSONSerialization JSONObjectWithData:... 时,返回的对象应该是NSArray。因此,当您调用 objectForKey: 时,您应该会看到一个异常。

如果您收到nil,那么唯一合理的结论是您要求解析的 JSON 数据格式不正确。

检查了您发布的NSData,您的服务器实际上正在返回:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>GBSB JSON Feed</title>
</head>
[{"date":"2012-08-13 12:00:00","timezone_type":3,"timezone":"EST5EDT"},{"date":"2012-08-13 10:00:00","timezone_type":3,"timezone":"EST5EDT"},{"date":"2012-08-13 13:00:00","timezone_type":3,"timezone":"EST5EDT"}]<body>

</body>
</html>

这显然不是有效的 JSON;这是一个恰好显示 JSON 的 HTML 文档。

【讨论】:

  • 我正在从服务器提取数据,它会改变。我不想硬编码json。有什么建议么?你是说我应该只有 php 部分,而 php 文件中没有标题或任何其他信息?
  • 如果它是您想要的有效 JSON,第一个字符将是 [{"date":"2012-08-13 12:,最后一个字符将是 _type":3,"timezone":"EST5EDT"}]。假设这正是您的 PHP 生成的内容,是的,只需转储它包含的所有 HTML 内容。
猜你喜欢
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 2019-02-18
相关资源
最近更新 更多