NSCFString 0x2749a0 valueForUndefinedKey此类不是与密钥数据编码兼容的密钥值 [英] NSCFString 0x2749a0 valueForUndefinedKey this class is not key value coding-compliant for the key data

查看:115
本文介绍了NSCFString 0x2749a0 valueForUndefinedKey此类不是与密钥数据编码兼容的密钥值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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.

I am using ios5, without ARC.

推荐答案

这意味着您的密钥路径不正确.处理数组时,您不能使用键路径,因为未知您需要数组的哪个项目.

This means that your key path is not correct. You can't use key path when dealing with arrays because it is unknown which item of the array you need.

您需要访问存储在键args下的NSArray,然后拉出第一个项目(字符串),然后将该字符串转换为NSDictionary,然后拉出.

You need to access the NSArray stored under the key args, then pull out the first item (which is a string), then convert that string into a NSDictionary, then pull out the key for data.

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

A quicker way is to make sure your JSON is formatted better:

{
  "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"}]
}

这篇关于NSCFString 0x2749a0 valueForUndefinedKey此类不是与密钥数据编码兼容的密钥值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆