ホームページ  >  に質問  >  本文

iOS 在jsonModel中如何获取嵌套数组的数据?

小弟的model定义如下:

@class cartitems;
@class giftItems;
@class specification;
@class promotions;



@interface THWShoppingCartModel : JSONModel
@property (nonatomic, assign) NSInteger discountPrice;//优惠立减

@property (nonatomic, assign) NSInteger quantity;//商品数量

@property (nonatomic, assign) NSInteger id;//ID

@property (nonatomic, assign) NSInteger effectiveQuantity;//有效商品数

@property (nonatomic, assign) NSInteger freightPrice;//运费

@property (nonatomic, assign) NSInteger effectivePrice;//有效金额

@property (nonatomic, strong) NSArray<giftItems*> *giftItems; //订单项

@property (nonatomic, strong) NSArray<cartitems*> *cartItems; //礼物项

@property (nonatomic, assign) NSInteger effectivePoint;//有效商品积分
@end


@interface cartitems : JSONModel

@property (nonatomic, assign) NSString* id; //订单ID

@property (nonatomic, assign) NSString *productId; //shangpinID

@property (nonatomic, copy) NSString *unit;//单位

@property (nonatomic, assign) NSString *quantity; //数量

@property (nonatomic, assign) NSString *discount;//折扣金额

@property (nonatomic, copy) NSString *tenantName;//商家名称

@property (nonatomic, copy) NSString *tenantId;//商家ID

@property (nonatomic, assign) NSString *price;//折后单价

@property (nonatomic, assign) NSString *subTotal;//金额小计

@property (nonatomic, copy) NSString *fullName;//全名

@property (nonatomic, copy) NSString *thumbnail;//缩略图

@property (nonatomic, strong) NSArray<specification*> *specification; //促销

@property (nonatomic, strong) NSArray<promotions*> *promotions; //规格

@property (nonatomic, assign) BOOL selected; //是否选择

@property (nonatomic, copy) NSString *name;//规格值

@property(nonatomic,copy) NSString *coupon;//是否可领卷

@end


@interface giftItems : JSONModel
@property(copy,nonatomic)NSString *id    ;                    //ID
@property (nonatomic, assign) NSUInteger productId    ;        //商品Id
@property(copy,nonatomic)NSString * fullName;                //全名
@property(copy,nonatomic)NSString *unit;                    //单位
@property(copy,nonatomic)NSString *thumbnail;                //缩略图
@property(copy,nonatomic)NSString *quantity;                //数量
@end


@interface specification : JSONModel
@property (nonatomic, assign) NSUInteger id;            //规格Id
@property(copy,nonatomic)NSString *name;                //规格名称
@end


@interface promotions : JSONModel
@property (nonatomic, assign) NSUInteger id;            //促销Id
@property(copy,nonatomic)NSString *name;                //促销名称
@end

现在需要用:

-(void)parseShoppingCartModelProfile:(NSArray*) cartItems
{
    if(!_moduleDic)
        _moduleDic = [NSMutableDictionary new];
    for (cartitems *item in cartItems) {
        @try {
            NSString *tentid = [NSString stringWithFormat:@"%@",item.tenantId];
            if(![_moduleDic.allKeys containsObject:tentid])
            {
                [_moduleDic setValue:item forKey:tentid];
            }
            else
            {
                [((NSMutableArray *)_moduleDic[tentid]) addObject:item];
            }
        } @catch (NSException *exception) {
            NSLog(@"%@",exception.description);
        }
    }
}

这个方法获取循环遍历处理THWShoppingCartModel NSArray<cartitems> cartItems 中每个成员的数据,但是无论如何都获取不到item.tenantId和其他cartItems model的数据,总是提示: -[__NSCFDictionary tenantId]: unrecognized selector sent to instance 0x7fdf9e0a3fa0,请问大神门如何处理?

巴扎黑巴扎黑2713日前685

全員に返信(4)返信します

  • ringa_lee

    ringa_lee2017-04-18 09:29:03

    投稿者のモデルはすべて JSONModel を継承していることがわかります。これはサードパーティの解析ツールですか?そうであれば、データを取得するためにトラバースする必要はありません。うまくいかない場合は、ブレークポイントに従ってデータを出力できます。見る。

    返事
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:29:03

    まず、エラーの内容によれば、プロジェクト内の CartItems の各要素が NSDictionary であることは比較的明らかです。

    対応するデータ ソースが正しいかどうかを確認することをお勧めします。

    返事
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:29:03

    1. これは辞書の中の辞書です

    2. これは辞書内の配列です

    特定の場所で使用する場合は、最も外側の jsonmodel オブジェクトを直接初期化するだけです。内部的にマッピングされます

    返事
    0
  • 黄舟

    黄舟2017-04-18 09:29:03

    jsonmodel が値の割り当てに失敗する場合があることがわかりました。他の自動解析ライブラリを使用することをお勧めします。

    返事
    0
  • キャンセル返事