【问题标题】:JSONModel Invalid JSON dataJSONModel 无效的 JSON 数据
【发布时间】:2013-11-15 18:44:23
【问题描述】:

我有这个来自 ASP MVC API 的 Json 我有 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 Code=1 “无效的 JSON 数据: 尝试使用 initWithDictionary:error 初始化 JSONModel 对象: 但字典参数不是 'NSDictionary'。"
UserInfo=0x109075ff0 {NSLocalizedDescription=无效的 JSON 数据: 尝试使用 initWithDictionary:error 初始化 JSONModel 对象: 但字典参数不是“NSDictionary”。, kJSONModelKeyPath=LotDTO}

这里是用于 LotDTO 的 JSONModel

#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

【问题讨论】:

  • 在我看来信息很清楚。你在哪里做 initWithDictionary?
  • 顺便说一句,你列出的不是字典。
  • 如果您在开头看到代码,我使用的是 initFromURLWithString 而不是字典。
  • 那么你在哪里做 initWithDictionary,字典长什么样?您的错误消息显示“尝试使用 initWithDictionary:error: 初始化 JSONModel 对象:但字典参数不是 'NSDictionary'”。
  • 这又是我想不通的原因?,我从来没有在我使用 initFromURLWithString 的帖子中查看过 initWithDictionary 的顶部代码。

标签: ios objective-c json jsonmodel


【解决方案1】:

我看到两个问题:

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

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

【讨论】:

  • 是的,关于 LotDTO 没有被定义为数组,你是对的,我到处乱砍我错过了那个,但我进行了测试和错误,在评论了所有属性并一一取消注释后我发现属性“描述”正在破坏提要,所以我只是将我的 JSON 重命名为“描述”,一切正常!可能是“描述”需要指向保留字吗?
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    请更换这个

    @property (strong, nonatomic) InvLotModel* LotDTO;
    

    有了这个

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

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

    这可能对你有帮助:Click here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多