JSONModel无效的JSON数据

时间:2013-11-15 18:44:23

标签: ios objective-c json jsonmodel

我有这个Json的ASP MVC API 我有InvModel和LotModel

但是当我打电话时

_InvFeed = [[InvModel alloc] initFromURLWithString:@"http://192.168.1.206/service/api/dto/inventory/1?p=Red%20Globe"
                                        completion:^(JSONModel *model, JSONModelError *err) 
        {
            NSLog(@"Inventory: %@", _InvFeed );
            NSLog(@"Error: %@",err);
        }];

我无法弄清楚这个错误:

  

错误:错误域= JSONModelErrorDomain代码= 1 “无效的JSON数据:   尝试使用initWithDictionary初始化JSONModel对象:错误:   但字典参数不是'NSDictionary'。“
  UserInfo = 0x109075ff0 {NSLocalizedDescription =无效的JSON数据:   尝试使用initWithDictionary初始化JSONModel对象:错误:   但是字典参数不是'NSDictionary'。,   kJSONModelKeyPath = LotDTO}

JSONData

以下是JSONModel:LotDTO

#import "JSONModel.h"

@interface InvLotModel : JSONModel
@property (assign, nonatomic) int lotid;
@property (strong, nonatomic) NSDate* expdate;
@property (strong, nonatomic) NSString* lotserial;
@property (strong, nonatomic) NSDate* lastupddate;
@property (strong, nonatomic) NSString<Optional>* providerlotserial;
@property (assign, nonatomic) NSDecimal* qtyoriginal;
@property (assign, nonatomic) NSDecimal* qtyallocated;
@property (assign, nonatomic) NSDecimal* qtyavailable;
@property (assign, nonatomic) NSDecimal* qtyonhand;
@property (strong, nonatomic) NSDate* receiptdate;
@property (strong, nonatomic) NSString* linecomment;
@property (assign, nonatomic) NSDecimal* unitcost;
@property (strong, nonatomic) NSString* warehouse;
@end

这里是库存模型

#import "JSONModel.h"
#import "InvLotModel.h"
@protocol InvModel @end
@interface InvModel : JSONModel
@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* itemid;
@property (strong, nonatomic) NSString* description;
@property (strong, nonatomic) NSDate* createdate;
@property (strong, nonatomic) NSString* createuser;
@property (assign, nonatomic) float lastcost;
@property (assign, nonatomic) BOOL monitorlevel;
@property (assign, nonatomic) int minlevel;
@property (assign, nonatomic) int maxlevel;
@property (strong, nonatomic) NSString* gtin;
@property (assign, nonatomic) float weight;
@property (strong, nonatomic) NSString* uom;
@property (strong, nonatomic) NSString* sizes;
@property (strong, nonatomic) NSString* variety;
@property (strong, nonatomic) NSString <Optional>* bag;
@property (strong, nonatomic) NSString* style;
@property (strong, nonatomic) NSString* box;
@property (strong, nonatomic) NSString* label;
@property (strong, nonatomic) NSString* commodity;

@property (strong, nonatomic) InvLotModel* LotDTO;
@end

3 个答案:

答案 0 :(得分:1)

我看到两个问题:

1)在InvModel类中,你将LotDTO定义为单个对象,而不是数组。

2)在您发布的JSON响应中,LotDTO的语法对我来说似乎不是有效的JSON。它似乎是一个LotDTO个对象的数组,但它不遵循JSON数组的语法(您可以检查,例如,here)。

答案 1 :(得分:0)

确保将任何扩展属性标记为Ignore。我试图复制模型时遇到了这样的问题。

答案 2 :(得分:0)

请替换此

@property (strong, nonatomic) InvLotModel* LotDTO;

用这个

@property (strong, nonatomic) NSArray<InvLotModel,ConvertOnDemand>* LotDTO;

正如JSONModel的官员建议在他们的一个教程中使用ConvertOnDemand将NSArray转换为JSONModelArray以避免实现中的错误。

这可能会对您有所帮助:Click here