【问题标题】:NSCFString 0x2749a0 valueForUndefinedKey this class is not key value coding-compliant for the key dataNSCFString 0x2749a0 valueForUndefinedKey 此类不符合键数据的键值编码
【发布时间】:2012-07-09 11:21:55
【问题描述】:

我的 json :

{
 "name": "notification",
 "args": [
         "{\"data\":  [{\"foreignId\":\"BF7E9276D8607DA5916F796F9F1B9743_2\",\"id\":\"\",\"img\":{\"small\":\"http://jiepang.com/static/img/icon-special-newbie.gif\"},\"poiId\":\"4fe133bb581f7129d6c3f2b3\",\"poiName\":\"Incubation Club Cafe - ICC\",\"source\":\"jiepang\",\"what\":\"aaaa。\",\"when\":\"\"}],\"size\":3,\"toWho\":[\"4ffa80c8e4b0f73fa2b758c9\"],\"type\":5,\"when\":\"2012-07-09T17:23:24Z\"}"
 ]
}

我的代码:

 NSDictionary* data=(NSDictionary *)[packet.data JSONValue];

NSString* str=[NSString stringWithFormat:@"%@",[data objectForKey:@"name"]];
txtview.text = [txtview.text stringByAppendingString:str];


NSData *jsonData = [packet.data dataUsingEncoding:NSUTF8StringEncoding];
__autoreleasing NSError* error = nil;
NSDictionary *resultdata = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
// NSDictionary* arr=[data valueForKeyPath:@"args.data"];

NSMutableDictionary *peopleListFromJson = [[NSMutableDictionary alloc] init];
peopleListFromJson = [resultdata valueForKeyPath:@"args.data"];
// NSArray *peopleListFromJson = [[result objectForKey:@"data"]objectForKey:@"list"];


if ( ![peopleListFromJson isKindOfClass:[NSArray class]] && peopleListFromJson!=nil) {
    peopleListFromJson =[NSArray arrayWithObject:peopleListFromJson];
}

for (NSDictionary *peopleFromJson in peopleListFromJson)
{

    if([peopleFromJson objectForKey:@"foreignId"]!=[NSNull null])
    {


        NSString* str=[NSString stringWithFormat:@"%@",[peopleFromJson objectForKey:@"foreignId"]];
        txtview.text = [txtview.text stringByAppendingString:str];
    }
}

但它给了我:

[<__NSCFString 0x2749a0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key data.'
*** First throw call stack:

我使用的是 ios5,没有 ARC。

【问题讨论】:

    标签: ios json sbjson


    【解决方案1】:

    这意味着您的密钥路径不正确。处理数组时不能使用key path,因为不知道需要数组的哪一项。

    您需要访问存储在密钥args下的NSArray,然后拉出第一项(这是一个字符串),然后将该字符串转换为NSDictionary,然后拉出@987654324的密钥@。

    更快的方法是确保您的 JSON 格式更好:

    {
      "name": "notification",
      "args": {"data": [{"foreignId": "BF7E9276D8607DA5916F796F9F1B9743_2", "id": "", "img":{"small": "http://jiepang.com/static/img/icon-special-newbie.gif"}, "poiId":"4fe133bb581f7129d6c3f2b3", "poiName":"Incubation Club Cafe - ICC","source":"jiepang","what":"aaaa。","when":""}],"size":3,"toWho":["4ffa80c8e4b0f73fa2b758c9"],"type":5,"when":"2012-07-09T17:23:24Z"}]
    }
    

    【讨论】:

    • 我的 nslog 结果数据是 NSDictionary
    • 忘记日志,调试器说什么?您可以只存储一个看起来像字典的字符串。
    • 调试器是:resultdata __NSCFConstantString * 0x3fa40580
    • 你去。那就是那里不是字典的证明。我还是编辑了我的答案,因为这有很多问题,尤其是 JSON
    • 我一步一步检查NSArray和NSDictionary或String,然后解析json。
    猜你喜欢
    • 2015-07-01
    • 2015-06-23
    • 2017-10-15
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多