Objective c Obj-C:如何将JSON反序列化为xyz模型对象数组集合?
我收到一个对象集合(如下所示) 以上代码给出以下错误Objective c Obj-C:如何将JSON反序列化为xyz模型对象数组集合?,objective-c,Objective C,我收到一个对象集合(如下所示) 以上代码给出以下错误 NSMutableArray* list = [[CouponDetailsModel alloc] initWithString:jsonData error:nil]; for (int i =0; i<[list count]; i++) { CouponDetailsModel* obj = (CouponDetailsModel*) [list objectAtIndex:i]; 2015-11-16 15:27:
NSMutableArray* list = [[CouponDetailsModel alloc] initWithString:jsonData error:nil];
for (int i =0; i<[list count]; i++) {
CouponDetailsModel* obj = (CouponDetailsModel*) [list objectAtIndex:i];
2015-11-16 15:27:33.825 myProj[2283:82450] -[__NSCFDictionary couponCode]: unrecognized selector sent to instance 0x60800026f2c0
2015-11-16 15:27:33.826 myProj[2283:82450] -[__NSCFDictionary couponCode]: unrecognized selector sent to instance 0x60800026f2c0
2015-11-16 15:27:33.964 myProj[2283:82450] (
0 CoreFoundation 0x00007fff8203403c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8cdae76e objc_exception_throw + 43
2 CoreFoundation 0x00007fff820370ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00007fff81f7ce24 ___forwarding___ + 1028
4 CoreFoundation 0x00007fff81f7c998 _CF_forwarding_prep_0 + 120
5 Common 0x00000001000bccba -[APIHelper getAvailableCoupons] + 362
6 myProj 0x0000000100005377 -[CouponsView windowDidLoad] + 423
7 AppKit 0x00007fff88ab6857 -[NSWindowController _windowDidLoad] + 586
8 AppKit 0x00007fff88a9eb83 -[NSWindowController window] + 110
9 myProj 0x000000010000278d -[ActivationScreenView showCoupons:] + 221
10 libsystem_trace.dylib 0x00007fff88578cd7 _os_activity_initiate + 75
11 AppKit 0x00007fff88b3eeb1 -[NSApplication sendAction:to:from:] + 452
12 AppKit 0x00007fff88b54946 -[NSControl sendAction:to:] + 86
13 AppKit 0x00007fff88b54862 __26-[NSCell _sendActionFrom:]_block_invoke + 131
14 libsystem_trace.dylib 0x00007fff88578cd7 _os_activity_initiate + 75
15 AppKit 0x00007fff88b547bf -[NSCell _sendActionFrom:] + 144
16 libsystem_trace.dylib 0x00007fff88578cd7 _os_activity_initiate + 75
17 AppKit 0x00007fff88b52cb3 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2821
18 AppKit 0x00007fff88bab34f -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 770
19 AppKit 0x00007fff88b51366 -[NSControl mouseDown:] + 714
20 AppKit 0x00007fff890bb2dc -[NSWindow _reallySendEvent:isDelayedEvent:] + 14125
21 AppKit 0x00007fff88a4ac86 -[NSWindow sendEvent:] + 470
22 AppKit 0x00007fff88a47212 -[NSApplication sendEvent:] + 2504
23 AppKit 0x00007fff88970b68 -[NSApplication run] + 711
24 AppKit 0x00007fff888ed244 NSApplicationMain + 1832
25 myProj 0x0000000100004a02 main + 34
26 myProj 0x0000000100001bb4 start + 52
27 ??? 0x0000000000000003 0x0 + 3
)
非常感谢您的帮助。非常感谢。使用NSJsonSerilization类将json对象转换为字典。。。。然后,从该字典中,您可以使用方法获取特定对象 [dicName objectforkey:Key]; 这将返回该键的数据。。。在您的情况下,此键是couponCollection
它将返回一个数组,您可以在其中获取优惠券的所有详细信息。您还没有告诉我们出了什么问题,也没有询问任何问题question@Cristik@Wain:非常感谢您的关注。我更新了我的问题并添加了我尝试过的内容。请喝一杯look@VikasBansal您在使用代码时遇到了哪些问题?什么不起作用?还请在问题中添加此细节。@Cristik谢谢您的建议。正如你所说,我已经再次更新了这个问题。我可以通过NSDictionary来完成,但是我想使用类模型对象来完成。有可能吗?您可以读取字典值,并可以在类变量上设置它们,以后可以在任何地方访问这些变量。
NSString* code = [obj couponCode]; // here is an error (given below)
}
2015-11-16 15:27:33.825 myProj[2283:82450] -[__NSCFDictionary couponCode]: unrecognized selector sent to instance 0x60800026f2c0
2015-11-16 15:27:33.826 myProj[2283:82450] -[__NSCFDictionary couponCode]: unrecognized selector sent to instance 0x60800026f2c0
2015-11-16 15:27:33.964 myProj[2283:82450] (
0 CoreFoundation 0x00007fff8203403c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8cdae76e objc_exception_throw + 43
2 CoreFoundation 0x00007fff820370ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00007fff81f7ce24 ___forwarding___ + 1028
4 CoreFoundation 0x00007fff81f7c998 _CF_forwarding_prep_0 + 120
5 Common 0x00000001000bccba -[APIHelper getAvailableCoupons] + 362
6 myProj 0x0000000100005377 -[CouponsView windowDidLoad] + 423
7 AppKit 0x00007fff88ab6857 -[NSWindowController _windowDidLoad] + 586
8 AppKit 0x00007fff88a9eb83 -[NSWindowController window] + 110
9 myProj 0x000000010000278d -[ActivationScreenView showCoupons:] + 221
10 libsystem_trace.dylib 0x00007fff88578cd7 _os_activity_initiate + 75
11 AppKit 0x00007fff88b3eeb1 -[NSApplication sendAction:to:from:] + 452
12 AppKit 0x00007fff88b54946 -[NSControl sendAction:to:] + 86
13 AppKit 0x00007fff88b54862 __26-[NSCell _sendActionFrom:]_block_invoke + 131
14 libsystem_trace.dylib 0x00007fff88578cd7 _os_activity_initiate + 75
15 AppKit 0x00007fff88b547bf -[NSCell _sendActionFrom:] + 144
16 libsystem_trace.dylib 0x00007fff88578cd7 _os_activity_initiate + 75
17 AppKit 0x00007fff88b52cb3 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2821
18 AppKit 0x00007fff88bab34f -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 770
19 AppKit 0x00007fff88b51366 -[NSControl mouseDown:] + 714
20 AppKit 0x00007fff890bb2dc -[NSWindow _reallySendEvent:isDelayedEvent:] + 14125
21 AppKit 0x00007fff88a4ac86 -[NSWindow sendEvent:] + 470
22 AppKit 0x00007fff88a47212 -[NSApplication sendEvent:] + 2504
23 AppKit 0x00007fff88970b68 -[NSApplication run] + 711
24 AppKit 0x00007fff888ed244 NSApplicationMain + 1832
25 myProj 0x0000000100004a02 main + 34
26 myProj 0x0000000100001bb4 start + 52
27 ??? 0x0000000000000003 0x0 + 3
)