使用json模型尝试从服务器获取数据到ios设备


Using JSONmodel to try and fetch data from a server to ios device

我一直在尝试使用JSONmodel来尝试从我的服务器到我的ios设备获取数据。我已经正确地设置了我的类,但由于某种原因,它在调用url后一直返回null。

feed = [[Feeds alloc] initFromURLWithString:@"http://http://www.cs4768project.net76.net/untitled.php?action=getShops"
                                         completion:^(JSONModel *model, JSONModelError *err) {
                                             NSLog(@"reached");
                                             //json fetched
                                             NSLog(@"shops: %@", feed.shops);

这是保存提要的模型

@interface Feeds : JSONModel
@property(strong,nonatomic) NSArray* shops;
@end

和我的咖啡店类一起

@interface CoffeeShop : JSONModel
@property(strong, nonatomic) NSString* name;
@property(nonatomic) CLLocationDegrees latitude;
@property(nonatomic) CLLocationDegrees longtitude;
@end

json输出

{"name":"Starbs","latitude":"45","longtitude":"-52"}

我一直在试图找到一个解决方案一段时间,并已提出了什么,我难倒了为什么这不起作用。

看起来JSON有问题因为我试过这段代码

NSError *e;
NSDictionary *s = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.cs4768project.net76.net/untitled.php?action=getShops"]] options:NSJSONReadingMutableLeaves error:&e];
NSLog(@"%@", s);
if (e) {
    NSLog(@"%@", e);
}

返回

Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0x1e0529f0 {NSDebugDescription=Garbage at end.}

所以,如果是你的网站,我会将JSON更改为有效格式,或者联系网站的所有者并通知他们这个问题。