简体   繁体   English

解析json并获取NSInvalidArgumentException',原因:'-[__ NSCFArray objectForKey:]:无法识别的选择器已发送到实例

[英]parsing json and getting NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance

I am trying to parse some json, so far I have: 我正在尝试解析一些json,到目前为止,我有:

 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSDictionary *results = [json objectForKey:@"d"];

    NSString *error = [results objectForKey:@"error"];
    NSArray *items = [results objectForKey:@"vehicles"];
  NSLog(@"items::%@", items);
    for (NSDictionary *item in items) {
        IDCard *idcard = [[IDCard alloc] init];
        idcard.year = [item objectForKey:@"year"];
        idcard.make = [item objectForKey:@"make"];
        idcard.model = [item objectForKey:@"model"];

the nslog of items is this: 项目的nslog是这样的:

items::(
        (
                {
            make = CHEV;
            model = MALIBU;
            year = 2002;
        },
                {

            make = GMC;
            model = SIERRA1500;
            year = 1995;
        }
    )
)

its fine til it gets to the idcard.year = [item objectForKey:@"year"]; 直到到达idcard.year = [item objectForKey:@"year"]; and it crashes with Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x9fdea80' and I dont understand why its doing this. 并且Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x9fdea80'Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x9fdea80'崩溃Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x9fdea80' ,我不明白为什么这样做。

If it helps to post the full json let me know and I will. 如果有助于发布完整的json,请告诉我,我会的。 Thank you for any help you can give me. 谢谢您能给我的任何帮助。

@ akashivskyy is right. @ akashivskyy是正确的。 You have an (seemingly redundant) array within an array (eg empty container array inside which the items array is residing). 您在一个数组中有一个(看似多余的)数组(例如,item数组所在的空容器数组)。 You need to look at modifying the JSON a bit, perhaps? 您可能需要稍微修改一下JSON? However, if you just want to avoid the NSInvalidArgumentException, the following should do the trick- instead of 但是,如果您只想避免NSInvalidArgumentException,则应使用以下技巧:

NSArray *items = [results objectForKey:@"vehicles"];

Try this: 尝试这个:

NSArray *items = [[results objectForKey:@"vehicles"] objectAtIndex:0];

If you look at the outputed json, you'll se that the dictionary is stored within an array. 如果查看输出的json,您会发现字典存储在数组中。

The array containing all the dictionaries is stored within another array of length 1. 包含所有字典的数组存储在另一个长度为1的数组中。

Replaced this line: 替换此行:

for (NSDictionary *item in items) {

with this line: 用这一行:

for (NSDictionary *item in [items objectAtIndex:0]) {

Its look like items is Array and contains Array of dictionary so if you perform operation objectForKey on NSArray will crash with error message "Unrecognized selector" 它看起来像项目是Array,并且包含字典的Array,因此,如果在NSArray上执行objectForKey操作,则会崩溃,并显示错误消息“无法识别的选择器”

try this. 尝试这个。

NSArray items1 =[items objectAtIndex:0];
for (NSDictionary *item in items1) {
    IDCard *idcard = [[IDCard alloc] init];
    idcard.year = [item objectForKey:@"year"];
    idcard.make = [item objectForKey:@"make"];
    idcard.model = [item objectForKey:@"model"];
 }

In case above solution not works then please paste your JSON string here 如果上述解决方案不起作用,请在此处粘贴您的JSON字符串

常见的陷阱可能使用arrayWithObjects代替arrayWithArray

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解析json并获取异常,原因:'-[__ NSCFArray objectForKey:]:无法识别的选择器已发送到实例0x7b1c7630 - parsing json and getting exception, reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7b1c7630 终止应用程序-'NSInvalidArgumentException',原因:'-[NSCFString objectForKey:]:无法识别的选择器已发送到实例 - Terminating app - 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__ NSCFArray length]:无法识别的选择器已发送到实例 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray length]: unrecognized selector sent to instance NSInvalidArgumentException”,原因:“-[__ NSCFConstantString objectForKey:]:无法识别的选择器已发送到实例0x10256a1b0” - NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x10256a1b0' 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__ NSArrayM objectForKey:]:无法识别的选择器已发送至实例 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 'NSInvalidArgumentException',原因:'-[NSTaggedPointerString objectForKey:]:无法识别的选择器已发送到实例0xa000000617461644' - 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString objectForKey:]: unrecognized selector sent to instance 0xa000000617461644' NSInvalidArgumentException,原因无法识别的选择器发送到实例 - NSInvalidArgumentException, reason unrecognized selector sent to instance NSInvalidArgumentException原因:无法识别的选择器发送到实例 - NSInvalidArgumentException reason : unrecognized selector sent to instance - [__ NSCFArray objectForKey:]:无法识别的选择器发送到实例0x11c824e0 - -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x11c824e0 MDC 因“NSInvalidArgumentException”而崩溃,原因:“-[UIButton applyOutlinedThemeWithScheme:]: 无法识别的选择器发送到实例 - MDC crashing with 'NSInvalidArgumentException', reason: '-[UIButton applyOutlinedThemeWithScheme:]: unrecognized selector sent to instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM