【发布时间】: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