0

我有两个 JSON 对象,它们是:

{
    "list1": [{
        "innerList": [{
            "innerListItem1": 50,
            "innerListItem2": 23,
            "innerListItem3": false,
            "innerListItem4": 43,
            "innerListItem5": 74
        }, {
            "innerListItem1": 22,
            "innerListItem2": 31,
            "innerListItem3": 90,
            "innerListItem4": 11,
            "innerListItem5": 27
        }]
    }]
}
{
    "innerList2": [{
        "innerList2Item1": 32,
        "innerList2Item2": 66,
        "innerList2Item3": 93,
        "innerList2Item4": 28,
        "innerList2Item5": true
    }, {
        "innerList2Item1": 23,
        "innerList2Item2": 94,
        "innerList2Item3": 6,
        "innerList2Item4": 0,
        "innerList2Item5": 18
    }]
}

我想innerListinnerList2Objective-C 和我的代码替换:

// here, I'm fetching a JSON object from user defaults and this is my list1
NSString *list1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"list1"];

// and here, I'm reading a JSON object from a JSON file and, above and this code works fine. I can read the both JSON objects.
NSString *list2 = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

// what I tried to access and assign the innerList2
NSString *oldList = [list1 valueForKeyPath:@"innerList"];
oldList = list2

但是 Xcode 在运行时抛出了这个异常:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x2839cd350> valueForUndefinedKey:]: this class is not key value coding-compliant for the key list1.'
*** First throw call stack:
(0x1fdf96180 0x1fd16e9f8 0x1fdeb3604 0x1fe97f3ac 0x1fe8f38cc 0x1003b6d14 0x100655c10 0x100655be8 0x100690aac 0x100655c34 0x1040e9460 0x1040ed01c 0x1040eabd0 0x1040eac18 0x1040eafb8 0x1040ea3bc 0x1040ecf34 0x1040ece3c 0x1040ed444 0x100691664 0x1006916b4 0x1040f8b18 0x1040f826c 0x1040f8118 0x1040f81e0 0x1040f91a0 0x22b48023c 0x1040f7f58 0x1040f8290 0x1040ed424 0x1040eb428 0x1040ea3bc 0x1040ecf34 0x1040ece3c 0x100499f3c 0x1004cd124 0x1004ccb98 0x1004cc53c 0x1004cc594 0x229c5ba20 0x229c5e354 0x229c5c9c0 0x22a20b1b8 0x22a1f9bfc 0x22a218868 0x1fdf27d6c 0x1fdf27670 0x1fdf226d4 0x1fdf21fb4 0x20012379c 0x22a1ffc38 0x100420a14 0x1fd9e58e0)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

我已经用谷歌搜索了这个例外,但问题和答案大多与 XIB 相关。
我的问题是这个异常的根本原因是什么,我该如何分配列表?

4

0 回答 0